Skip to main content

htpy - HTML in Python

htpy is a library that makes writing HTML in plain Python fun and efficient, without a template language.

Define HTML in Python:

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

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

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

And Get HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Today's menu</title>
  </head>
  <body>
    <h1>Menu</h1>
    <ul class="menu">
      <li>egg+bacon</li>
      <li>bacon+spam</li>
      <li>eggs+spam</li>
    </ul>
  </body>
</html>

Motivation for This Project

At Personalkollen, where htpy was originally developed we often found ourselves hitting walls when using classic templates. htpy was created to improve the productiveness and experience of generating HTML from a Python backend.

Key Features

  • Leverage static types: Use mypy or pyright to type check your code.

  • Great debugging: Avoid cryptic stack traces from templates. Use your favorite Python debugger.

  • Easy to extend: There is no special way to define template tags/filters. Just call regular functions.

  • Works with existing Python web framework: Works great with Django, Flask or any other Python web framework!

  • Full async support: Render HTML fully async with ASGI frameworks such as Starlette and FastAPI.

  • Works great with htmx: htpy makes for a great experience when writing server rendered partials/components.

  • Create reusable components: Define components, snippets, complex layouts/pages as regular Python variables or functions.

  • Familiar concepts from React: React helped make it popular writing HTML with a programming language. htpy uses a lot of similar constructs.

Philosophy

htpy generates HTML elements and attributes and provide a few helpers.

htpy does not enforce any particular pattern or style to organize your pages, components and layouts. That does not mean that htpy cannot be used to build sophisticated web pages or applications.

Rather the opposite: you are encouraged the leverage the power of Python to structure your project. Use modules, classes, functions, decorators, list comprehension, generators, conditionals, static typing and any other feature of Python to organize your components. This gives you a lot of power and makes htpy scale from a single small Flask project to bigger applications.

Common patterns can give you some ideas that you can build upon yourself.

The Syntax

Child elements are specified using the [] syntax. This may look strange at first but it has some nice benefits. This clearly separates attributes from child elements and makes the code more readable. It is implemented using the __getitem__ method, just like lists or dicts.

Installation

htpy is available on PyPI. You may install the latest version using pip:

pip install htpy

Documentation

The full documentation is available at htpy.dev:

Release files for htpy 26.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for htpy 26.5.1
File Size Uploaded
htpy-26.5.1.tar.gz 292.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for htpy 26.5.1
File Interpreter ABI Platform
htpy-26.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 313.5 kB

Release files / htpy-26.5.1.tar.gz

Download URL htpy-26.5.1.tar.gz
Size 292.0 kB
Tags Source
SHA-256 checksum
How to use checksums
43a365c1fc670094da781b923883019339cec031397d61d5fe5f8e2f2278b63e
BLAKE2b-256 checksum
How to use checksums
d31848b0fe5f7b23c8bb0b45b09c306f511417fee74afc49a0765178d6dcbac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 22, 2026.

Transparency log

Release files / htpy-26.5.1-py3-none-any.whl

Download URL htpy-26.5.1-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
148566db9d720c897baa5832e2a496954ce317ab06ef49a103b44f8a15322c3b
BLAKE2b-256 checksum
How to use checksums
e768ad12d6519ccc48852ebb1effbe5a160ff21d7b48c484d0dae6fd173f6d22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 22, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

26.5.1 This release

2 release files

26.5.0

2 release files

25.8.1

2 release files

25.5.1

2 release files

25.5.0

2 release files

25.4.2

2 release files

25.4.1

2 release files

25.4.0

2 release files

25.3.0

2 release files

25.1.0

2 release files

24.8.3

2 release files

24.8.2

2 release files

24.8.1

2 release files

24.7.2

2 release files

24.7.1

2 release files

24.6.1

2 release files

24.6.0

2 release files

24.4.0

2 release files

24.3.9

2 release files

24.3.8

2 release files

24.3

2 release files

24.1

2 release files

23.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page