Skip to main content

htpy - HTML in Python

Project description

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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

htpy-25.12.0.tar.gz (291.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

htpy-25.12.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file htpy-25.12.0.tar.gz.

File metadata

  • Download URL: htpy-25.12.0.tar.gz
  • Upload date:
  • Size: 291.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for htpy-25.12.0.tar.gz
Algorithm Hash digest
SHA256 7d3f4aaa10b35c5e46dfa804df1f3f18772caf8efee6e6a035b5dee89a5d6af8
MD5 814c06b28ba191905a9a989e69aff486
BLAKE2b-256 b623e00bbc355e70444d16c90a0f1fdce108c67379fe65e9312cd026c13db976

See more details on using hashes here.

Provenance

The following attestation bundles were made for htpy-25.12.0.tar.gz:

Publisher: release.yml on pelme/htpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htpy-25.12.0-py3-none-any.whl.

File metadata

  • Download URL: htpy-25.12.0-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for htpy-25.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 642e69278d6f8f4643acc2d2d13c21682ceb5fb4860ecbbce042f171577fff54
MD5 d41df42ac443e62f699a5dbd180a76fc
BLAKE2b-256 61f1a2f2caf14b03e7fab4801ac6018a4ac996de3e82a573e7aa21f3cb11a7cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for htpy-25.12.0-py3-none-any.whl:

Publisher: release.yml on pelme/htpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page