Registry / web-framework / yattag

yattag

JSON →
library1.16.1pypypi✓ verified 10d ago

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.

pip install yattag
INSTALL
IMPORT
SIG · YATTAG
Y
yattag
web-frameworkpythonv1.16.1
Install
2.4s avg
Import
14ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.16.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.014s · 19.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.014s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Doc
from yattag import Doc
tag, text
from yattag import Doc; doc, tag, text = Doc().tagtext()
from yattag import tag, text
`tag` and `text` are methods obtained from a `Doc` instance, typically via `Doc().tagtext()` for convenience, not direct imports.
indent
from yattag import indent

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.

from yattag import Doc doc, tag, text = Doc().tagtext() with tag('html'): with tag('head'): with tag('title'): text('My Yattag Page') with tag('body', id='main-body', klass='container'): with tag('h1'): text('Hello Yattag World!') with tag('p'): text('This is a paragraph generated programmatically.') result_html = doc.getvalue() print(result_html) # To get indented output for readability: from yattag import indent indented_html = indent(result_html) print('\n--- Indented HTML ---') print(indented_html)
Debug
Known issues
gotchaWhen setting HTML `class` attributes, use `klass` as the keyword argument in Python to avoid conflicts with the `class` reserved keyword.
fix
Instead of `with tag('div', class='my-class')`, use `with tag('div', klass='my-class')`.
affects: All versions
gotchaFor HTML/XML attributes containing hyphens (e.g., `data-id`, `ng-app`), pass them as `(key, value)` tuples because Python keyword arguments cannot contain hyphens.
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')`.
affects: All versions
gotchaThe `text()` method automatically escapes HTML special characters (`&`, `<`, `>`, etc.). If you need to insert raw HTML or XML content without escaping, use the `asis()` method.
fix
For raw content, use `doc.asis('<script>alert("hello");</script>')` instead of `text('<script>alert("hello");</script>')`.
affects: All versions
gotchaBy 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.
fix
After generating your document, pass the result to `indent()`: `print(indent(doc.getvalue()))`. You can also configure indentation options.
affects: All versions
Upgrade
Version history
1.16.1latest on PyPI · released Nov 2, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
18
Amazon
1
Resources
yattag — pip install yattag · libregistry