Registry / web-framework / htbuilder

htbuilder

JSON →
library0.9.0pypypi✓ verified 84d ago

htbuilder is a Python library for building HTML strings using a purely functional syntax, akin to JSX rather than traditional templating engines. It allows developers to construct HTML elements and attributes using Python functions, providing a clean and programmatic way to generate markup. The library is currently active, with its latest version being 0.9.0, released in September 2023.

pip install htbuilder
INSTALL
IMPORT
SIG · HTBUILDER
H
htbuilder
web-frameworkpythonv0.9.0
Install
2.4s avg
Import
10ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.012s · 19.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.009s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

div
from htbuilder import div
H
from htbuilder import H
H is an alias for HtmlElement, often used for convenience to access common HTML tags.
styles
from htbuilder import styles
Used to define inline CSS styles for HTML elements.
px
from htbuilder.units import px
Imports unit helpers for CSS properties, e.g., `px(10)`.

This quickstart demonstrates how to create basic HTML elements, add text content, include attributes, and nest elements using `htbuilder`'s functional syntax. It shows both direct tag imports and the `H` factory for common tags.

from htbuilder import div, p, a, H # Create a simple div with text hello_world = div('Hello, World!') print(hello_world.render()) # Expected: <div>Hello, World!</div> # Create an element with attributes and children link_element = a(href='https://github.com/tvst/htbuilder')('htbuilder GitHub') paragraph_element = p('Visit the ', link_element, ' for more info.') # Nest elements using the H (HtmlElement) factory for common tags page_content = H.div(id='main-content')( H.h1('Welcome'), paragraph_element ) print(page_content.render())
Debug
Known issues
gotchaWhen defining HTML tag names or attributes that contain dashes (e.g., `my-element`, `foo-bar`), use underscores instead. Python identifiers do not support dashes, and `htbuilder` automatically converts underscores to dashes during rendering.
fix
Instead of `my-element(foo-bar='value')`, use `my_element(foo_bar='value')`.
affects: All versions
gotchaIf you need to use a Python reserved keyword (like `class` or `for`) as an HTML attribute name, prefix it with an underscore. `htbuilder` will strip the leading underscore before rendering the HTML.
fix
Instead of `div(class='my-class')`, use `div(_class='my-class')`.
affects: All versions
gotchahtbuilder is designed for a purely functional approach to HTML generation, similar to JSX. It is not a templating engine like Jinja2 or Django Templates. Expect to write Python code for all HTML structure rather than using string-based templates.
fix
Embrace the functional Python syntax for defining elements, attributes, and children. Avoid trying to embed template-like logic.
affects: All versions
Errors
Common errors & fixes
SyntaxError: invalid syntax (when using dash in tag or attribute name)
Python does not allow hyphens in variable or function names, which are used to represent HTML tags/attributes in `htbuilder`.
fix
Replace hyphens with underscores. E.g., `from htbuilder import my_component` instead of `my-component`, and `div(data_attribute='value')` instead of `div(data-attribute='value')`.
TypeError: __call__() got an unexpected keyword argument 'class'
Attempting to use a Python reserved keyword like `class` directly as an HTML attribute name without escaping it.
fix
Prefix the reserved keyword with an underscore. For `class`, use `_class`. Example: `div(_class='my-css-class')`.
Upgrade
Version history
0.9.0latest on PyPI · released Jan 9, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
10
Resources
htbuilder — pip install htbuilder · libregistry