skillfed

htpy

htpy - HTML in Python

htpy v26.5.1 207.0K downloads/30d#9,561 on PyPI477
Permissive license MIT Active released

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 htpy

uv

uv add htpy

poetry

poetry add htpy

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: Python :: 3Topic :: Text Processing :: Markup :: HTMLTyping :: Typed

Tags

HTML generation in Pythonwrite HTML with Python codetemplate-free HTML builderPython HTML DSLserver-side HTML renderinghtmx-friendly HTML generationasync HTML rendering
html-generationtype-safeasync-ready

More HTML packages