Python Hiccup
Python Hiccup is a library for representing HTML using plain Python data structures.
What is Python Hiccup?
This is a Python implementation of the Hiccup syntax. Python Hiccup is a library for representing HTML in Python.
Using list or tuple to represent HTML elements, and dict to represent the element attributes.
This project started out as a fun coding challenge, and now evolving into something useful for Python Dev teams.
Usage
Create server side HTML using plain Python data structures. You can also use it with PyScript.
Example
Python:
from python_hiccup.html import render
render(["div", "Hello world!"])
The output will be a string: <div>Hello world!</div>
With Hiccup, you can create HTML in a programmatic style. To render HTML like:
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
with Python:
def todo(data: list) -> list:
return [["li", i] for i in data]
data = todo(["one", "two", "three"])
render(["ul", data])
Basic syntax
Python:
["div", "Hello world!"]
The HTML equivalent is:
<div>Hello world!</div>
Writing a nested HTML structure, using Python Hiccup:
["div", ["span", ["strong", "Hello world!"]]]
The HTML equivalent is:
<div>
<span>
<strong>Hello world!</strong>
</span>
</div>
Adding attributes to an element, such as CSS id and classes, using Python Hiccup:
["div", {"id": "foo", "class": "bar"}, "Hello world!"]
or, using a more concise syntax:
["div#foo.bar", "Hello world!"]
The HTML equivalent is:
<div id="foo" class="bar">Hello world!</div>
Adding valueless attributes to elements, such as the async or defer, by using Python set:
["!DOCTYPE", {"html"}]
["script", {"async"}, {"src": "js/script.js"}]
The HTML equivalent is:
<!DOCTYPE html>
<script async src="js/script.js"></script>
Adding unescaped content
This is useful when rendering HTML entities like ©.
from python_hiccup.html import raw
data = ["div", raw("© this should <strong>not</strong> be escaped!")]
The HTML output:
<div>© this should <strong>not</strong> be escaped!</div>
Resources
- PyScript and python-hiccup example - PyScript Jokes with a Hiccup
- Hiccup - the original implementation, for Clojure.
Existing python alternatives
Development
Running lint:
uv run ruff check
Running type checks:
uv run ty check
Running tests:
uv run pytest
Metadata
Release files for python-hiccup 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_hiccup-0.4.1.tar.gz | 20.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_hiccup-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.8 kB
Release files / python_hiccup-0.4.1.tar.gz
| Download URL | python_hiccup-0.4.1.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
690d4eebedf17f9f886bbe0b8b291720a2a30edf7406b2ff31bb91ec9a28c689
|
|
BLAKE2b-256 checksum How to use checksums |
c507f0425a923abbc64744da1ae3abf4c01f0d53585e95140c7e4c6a950e1459
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.0
|
Release files / python_hiccup-0.4.1-py3-none-any.whl
| Download URL | python_hiccup-0.4.1-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b6949ed8ca90d4103a39fd2f487b2a5ca4d984bc0701661359a9977459d92bca
|
|
BLAKE2b-256 checksum How to use checksums |
d2b90566c3fff5211dbc99311274cc68ec30a19995136a3b6d390f0518452521
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.0
|