Skip to main content

htmlkit icon with cyan and magenta angle brackets

htmlkit

Write HTML in Python.

The htmlkit package allows developers to skip Jinja templates and use familiar Python tools like linters, formatters, and debuggers to build web apps more easily.

from htmlkit import body, h1, head, html, p, title

page = html(
    head(title("Hello from Python")),
    body(
        h1("Hello from Python", id="banner", style="font-style: italic;"),
        p("Write HTML in Python with htmlkit.", class_="intro"),
    ),
    lang="en",
)

print(page)

The formatted print output is:

<!doctype html>
<html lang="en">
  <head>
    <title>Hello from Python</title>
  </head>
  <body>
    <h1 id="banner" style="font-style: italic">Hello from Python</h1>
    <p class="intro">Write HTML in Python with htmlkit.</p>
  </body>
</html>

Installation

Requires Python 3.14 or newer.

Install htmlkit with the awesome uv package manager:

uv add htmlkit

Or with pip:

pip install htmlkit

Or install the development version directly from GitHub:

uv add git+https://github.com/wigging/htmlkit

Elements and content

Use input_, map_, object_, and del_ for the corresponding HTML tags that conflict with reserved Python objects; svg and math are not included. For custom tags, import Element and use Element("my-widget", "Text"). Void elements such as meta, link, and input do not accept children.

Children can be text, integers, elements, fragments, or nested lists and tuples; None is ignored. Text and attribute values are escaped automatically, writing &amp;, &lt;, &gt;, &#34;, and &#39; produces the following:

from htmlkit import p

str(p("A < B & C"))
# '<p>A &lt; B &amp; C</p>'

str(p("Note", class_="note", data_id=7))
# '<p class="note" data-id="7">Note</p>'

Attributes are keywords, not positional mappings. A trailing underscore is stripped, then internal underscores become hyphens: class_ becomes class, for_ becomes for, data_id becomes data-id, and http_equiv becomes http-equiv. Attribute names become lowercase, so ID and id refer to the same attribute. True adds a bare attribute; False and None omit it. Supplying two keywords that normalize to the same attribute name raises TypeError. String, integer, and floating-point attribute values are supported; numbers use Python's str() representation, for example step=0.01 becomes step="0.01". Tag names, attribute names, and executable attribute values must be trusted: escaping is not sanitization.

Render with str(element) or print(element). The html`` element adds the HTML5 doctype`. Calling an existing element creates a new one, merging attributes and replacing children.

Lists and fragments

Use repeat() to create one element per item, sharing the same attributes:

from htmlkit import ul, li

items = ul(li.repeat(["Apples", "Pears & peaches"]))

row = li(class_="result")
results = ul(row.repeat(["Alice", "Bob"]))

Note that repeat() returns a Fragment and void elements do not support it unless they hold placeholders, as described next.

For rows with several values per item, build the element once with each placeholders and let repeat() fill them from every item. each stands for the item being rendered: each.name reads its name attribute, each[key] indexes it, each(callable) calls a function with it, and each alone is the item itself. Placeholders are string subclasses that retain their accessors. Use them directly as children or attribute values, or combine them with +:

from dataclasses import dataclass

from htmlkit import a, each, table, tbody, td, th, thead, tr


@dataclass
class Item:
    name: str
    status: str
    href: str
    archived: bool


items = [
    Item(name="Widget", status="active", href="/items/1", archived=False),
    Item(name="Gizmo", status="paused", href="/items/2", archived=True),
]

row = tr(
    td(each.name, class_="status " + each.status),
    td(a("Edit", href=each.href), hidden=each.archived),
)

page = table(
    thead(tr(th("Name"), th("Action"))),
    tbody(row.repeat(items)),
)

print(page)

The print output, shown here with formatting added, fills the template once per item:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="status active">Widget</td>
      <td><a href="/items/1">Edit</a></td>
    </tr>
    <tr>
      <td class="status paused">Gizmo</td>
      <td hidden><a href="/items/2">Edit</a></td>
    </tr>
  </tbody>
</table>

A whole bound attribute follows the usual rules: None and False omit it,True renders only its name, as hidden does above. Bound values are escaped like children, so an element or a repeat() result can be bound to nest loops. each("owner.name") reads a dotted path, and bind() is the function behind each for code that prefers it. A template's HTML is compiled on its first repeat(), so rows cost far less than building each element; an element with placeholders is meant for repeat(), and rendering it directly leaves the placeholders in the output.

Placeholders and templates keep their callbacks alive for reuse. Once they are discarded, htmlkit no longer retains the callbacks or their captured objects; completed repeat() results do not retain them. Use + rather than f-strings, str.join(), or other operations that discard the string subclass and its accessor references. repeat() raises ValueError if a placeholder has lost its accessor reference. Alternatively, put string formatting inside a callback, such as each(lambda item: f"status {item.status}").

Use Fragment to group children without a wrapping tag, or from_iterable() to collect a generator:

from htmlkit import Fragment, h1, p

content = Fragment(h1("Welcome"), p("A & B"))
paragraphs = Fragment.from_iterable(p(name) for name in ["Alice", "Bob"])

Both repeat() and from_iterable() consume their input immediately.

Trusted HTML

Use Raw for trusted markup that must not be escaped, such as scripts and styles:

from htmlkit import Raw, head, script, style

head(
    style(Raw("body > main { margin: 0 auto }")),
    script(Raw("if (a < b && c) { go(); }")),
)

Raw is not sanitized. Child objects with an __html__() method also bypass escaping. The Element, Fragment, and Raw objects support this protocol for use with MarkupSafe, Jinja, and Django. Pass elements, fragments, and Raw directly as children. Converting them to str first causes their HTML to be escaped again.

FastAPI web app

In a FastAPI web application, use htmlkit instead of Jinja templates for HTML responses. An htmlkit element can be passed directly to the HTMLResponse as demonstrated below.

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from htmlkit import html, body, h1, p

app = FastAPI()


@app.get("/", response_class=HTMLResponse)
def index():
    page = html(body(h1("Hello"), p("Built with htmlkit.")))
    return HTMLResponse(page)

Contributing

Contribute to this project by submitting a pull request or issue. Pull requests should pass all checks and tests before submitting.

uv sync
uv run ruff check .
uv run ruff format --check .
uv run ty check
uv run pytest

Metadata

Release files for htmlkit 26.11

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

Source distribution (sdist)

Source distribution for htmlkit 26.11
File Size Uploaded Provenance Actions
htmlkit-26.11.tar.gz 14.6 kB Without provenance Details for htmlkit-26.11.tar.gz

Built distribution (wheel)

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

Total release size: 29.8 kB

Release files / htmlkit-26.11.tar.gz

Download URL htmlkit-26.11.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2e52b77ea40653a14ccda2fb36a8fb9b8616f0c86552c558de2abe04d0e32397
BLAKE2b-256 checksum
How to use checksums
b87935e640181827862b73242e60f6a95e9abdea02a31832a2908a5e14448d82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / htmlkit-26.11-py3-none-any.whl

Download URL htmlkit-26.11-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b9c58ae1774f8e2b9b0665b7a46428755c08aa417ea36f81d19c375e8e4b565
BLAKE2b-256 checksum
How to use checksums
17f5247f6c09535b4f44b74c86d88dff7ce905bdad78dcb10354fc3782d895e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for htmlkit 26.11

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

26.11 This release

2 release files No provenance

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