Python Hiccup
Python Hiccup is a library for representing HTML and XML 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 and XML in Python.
Using list or tuple to represent HTML or XML elements, and dict to represent the element attributes.
Usage
Create server side HTML using plain Python data structures. You can also use it with PyScript.
Example, creating HTML
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])
Example, creating XML
Python:
from python_hiccup.xml import render
render(["Message", "Hello world!"])
The output will be a string: <Message>Hello world!</Message>
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>
Creating an XML document
attributes = {"xmlns:fictional": "http://characters.example.com", "xmlns": "http://people.example.com"}
john = ["actor", [["name", "John Cleese"], ["fictional:character", "Archie Leach"]]]
eric = ["actor", [["name", "Kevin Kline"], ["fictional:character", "Otto West"]]]
actors = ["actors", attributes, [john, eric]]
document = [["?xml", {"version": 1.0}], actors]
The rendered output:
<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com" xmlns="http://people.example.com">
<actor>
<name>John Cleese</name>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Kevin Kline</name>
<fictional:character>Otto West</fictional:character>
</actor>
</actors>
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 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_hiccup-1.2.0.tar.gz | 22.9 kB | Without provenance | Details for python_hiccup-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_hiccup-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for python_hiccup-1.2.0-py3-none-any.whl |
Total release size: 31.6 kB
Release files / python_hiccup-1.2.0.tar.gz
| Download URL | python_hiccup-1.2.0.tar.gz |
|---|---|
| Size | 22.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc510cef29c93a030987e6d00cbf56064419fa475b1ee8298ab10551858dfd50
|
|
BLAKE2b-256 checksum How to use checksums |
663e598c70cb376978f6e8d53cc5b6b20f8d6d6c36c339eaa3c9848fc5a465b2
|
| 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-1.2.0-py3-none-any.whl
| Download URL | python_hiccup-1.2.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12632d77354f43be2b5e15b260f8f2175475c0412db4bbc88d44bf17641107b7
|
|
BLAKE2b-256 checksum How to use checksums |
a5dc31f16cebe8db7579dd6a190de0785d927dc4a941d7bb79a6f1a98bf0c027
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.0
|
Security for python-hiccup 1.2.0
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.