{"id":4863,"library":"yattag","title":"Yattag","description":"Yattag is a Python library for generating HTML or XML in a pythonic way. It offers a pure Python alternative to web template engines, allowing developers to create dynamic HTML documents programmatically. A notable feature is its ability to fill HTML forms with default values and error messages. The current stable version is 1.16.1, released on November 2, 2024, with an active but irregular release cadence.","status":"active","version":"1.16.1","language":"en","source_language":"en","source_url":"https://github.com/leforestier/yattag","tags":["html generation","xml generation","template engine alternative","forms","declarative html"],"install":[{"cmd":"pip install yattag","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"Doc","correct":"from yattag import Doc"},{"note":"`tag` and `text` are methods obtained from a `Doc` instance, typically via `Doc().tagtext()` for convenience, not direct imports.","wrong":"from yattag import tag, text","symbol":"tag, text","correct":"from yattag import Doc; doc, tag, text = Doc().tagtext()"},{"symbol":"indent","correct":"from yattag import indent"}],"quickstart":{"code":"from yattag import Doc\n\ndoc, tag, text = Doc().tagtext()\n\nwith tag('html'):\n    with tag('head'):\n        with tag('title'):\n            text('My Yattag Page')\n    with tag('body', id='main-body', klass='container'):\n        with tag('h1'):\n            text('Hello Yattag World!')\n        with tag('p'):\n            text('This is a paragraph generated programmatically.')\n\nresult_html = doc.getvalue()\nprint(result_html)\n\n# To get indented output for readability:\nfrom yattag import indent\nindented_html = indent(result_html)\nprint('\\n--- Indented HTML ---')\nprint(indented_html)","lang":"python","description":"This example demonstrates how to create a basic HTML document using Yattag's `Doc`, `tag`, and `text` methods. It also shows how to apply attributes, including the special `klass` for HTML `class`, and how to use the `indent` function for more readable output."},"warnings":[{"fix":"Instead of `with tag('div', class='my-class')`, use `with tag('div', klass='my-class')`.","message":"When setting HTML `class` attributes, use `klass` as the keyword argument in Python to avoid conflicts with the `class` reserved keyword.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Instead of `with tag('div', data-id='123')`, use `with tag('div', ('data-id', '123'))`. For boolean attributes, you can use `with tag('html', 'ng-app')` or `with tag('script', defer='defer')`.","message":"For HTML/XML attributes containing hyphens (e.g., `data-id`, `ng-app`), pass them as `(key, value)` tuples because Python keyword arguments cannot contain hyphens.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For raw content, use `doc.asis('<script>alert(\"hello\");</script>')` instead of `text('<script>alert(\"hello\");</script>')`.","message":"The `text()` method automatically escapes HTML special characters (`&`, `<`, `>`, etc.). If you need to insert raw HTML or XML content without escaping, use the `asis()` method.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After generating your document, pass the result to `indent()`: `print(indent(doc.getvalue()))`. You can also configure indentation options.","message":"By default, `doc.getvalue()` returns a compact, non-indented string. This is efficient but can be unreadable. To get human-readable, indented output, use the `indent` function.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}