htpy - HTML in Python

25.12.0 · active · verified Thu Apr 16

htpy is a Python library designed for generating HTML in plain Python code, eliminating the need for separate templating languages. It aims to make writing HTML fun and efficient, leveraging Python's features like static typing and debugging. The current version is 25.12.0, and it follows a frequent release cadence, often monthly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a simple HTML page with a head, title, body, heading, and an unordered list dynamically generated from a Python list. The `print(page)` call renders the entire structure to an HTML string.

from htpy import body, h1, head, html, li, title, ul

menu = ["egg+bacon", "bacon+spam", "eggs+spam"]

page = html[
    head[title["Today's menu"]],
    body[
        h1["Menu"],
        ul(".menu")[(li[item] for item in menu)],
    ],
]

print(page)

view raw JSON →