Yattag

1.16.1 · active · verified Sun Apr 12

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.

Warnings

Install

Imports

Quickstart

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)

view raw JSON →