htpy
htpy - HTML in Python
What it is and what it does
htpy is a Python library for generating HTML without templates. Instead of writing HTML strings or using a template language, you define HTML elements and structure using Python syntax—nesting child elements with brackets, passing attributes as function arguments, and composing components as regular Python functions or variables. It relies on markupsafe for safe HTML escaping and typing_extensions for type hints.
The library is designed to work with any Python web framework (Django, Flask, etc.) and supports async rendering for ASGI frameworks. Because HTML is just Python code, you can use static type checking with mypy or pyright, debug with standard Python debuggers, and leverage Python's full feature set—functions, classes, comprehensions, conditionals—to organize and reuse components. It was created to avoid the friction of template languages while maintaining the familiarity of Python development.
Use it for:
- Build server-rendered HTML components for htmx-driven interfaces without switching to a template language.
- Generate complex, nested HTML structures in Flask or Django views using Python functions and comprehensions.
- Type-check your HTML generation code with mypy or pyright to catch structural errors early.
- Render HTML asynchronously in FastAPI or Starlette applications with full async/await support.
- Create reusable HTML components as Python functions or classes that compose like regular code.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
htpy lets you write HTML directly in Python using bracket syntax and Python expressions, generating valid HTML without a template language.
Yes. htpy is actively maintained, has low install friction, carries no known vulnerabilities, and uses a permissive MIT license. It is well-suited if you prefer writing HTML in Python over templates and want static typing and debugging support. Not necessary if you are already satisfied with your templating approach or need Python versions below 3.10.
Install
htpy on PyPI
pip
pip install htpyuv
uv add htpypoetry
poetry add htpyInstalling htpy
Before you install
Low friction: pure Python wheel with only two lightweight runtime dependencies (markupsafe, typing_extensions). Active maintenance with recent release 84 days ago and 477 repository stars.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open and closed projects with minimal restrictions.
Quickstart
pip install htpy
from htpy import html, head, body, title, h1, ul, li
result = html[
head[title["Example"]],
body[h1["Hello"], ul[(li[item] for item in ["one", "two"])]]
]
Requires Python 3.10 or later.
Verify before relying
- Whether the package's async support integrates seamlessly with specific ASGI frameworks beyond Starlette and FastAPI.
- Performance characteristics when rendering large or deeply nested HTML structures.
- Compatibility with type checkers other than mypy and pyright.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — markupsafe, typing_extensions |
| Maintenance | actively maintained — 84 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 206,954/month — #9,561 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: htpy-26.5.1-py3-none-any.whl
Tags
More HTML packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
beautifulsoup4Beautiful Soup parses HTML and XML documents…
permissive · top 100 on PyPI
lxmllxml provides Python bindings to libxml2 and…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
MarkdownConverts Markdown text to HTML using a Python…
permissive · top 1,000 on PyPI
htbuilderhtbuilder lets you construct HTML strings using…
permissive · top 15,000 on PyPI
tinyhtmlRenders HTML5 safely from Python expressions…
permissive · top 15,000 on PyPI
textileConverts Textile markup (a human-friendly text…
permissive · top 15,000 on PyPI
html5taggerGenerates HTML5 markup programmatically using…
permissive · top 5,000 on PyPI
dominateDominate is a Python library for writing HTML…
copyleft · top 5,000 on PyPI
python-fasthtmlFastHTML is a Python web framework that builds…
permissive · top 5,000 on PyPI
htmlBuilderRender HTML by writing Python code, using…
permissive · top 15,000 on PyPI
html-textExtracts plain text from HTML while filtering…
permissive · top 5,000 on PyPI
tabulateFormats and prints tabular data in plain text…
permissive · top 1,000 on PyPI
hyperscriptHyperscript lets you generate HTML markup…
permissive · top 15,000 on PyPI