Skip to main content

Python Hiccup

Python Hiccup is a library for representing HTML and XML using plain Python data structures.

CircleCI

CodeScene Average Code Health

Quality Gate Status

Download Stats

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 &copy;.

from python_hiccup.html import raw

data = ["div", raw("&copy; this should <strong>not</strong> be escaped!")]

The HTML output:

<div>&copy; 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

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.1.0

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

Source distribution (sdist)

Source distribution for python-hiccup 1.1.0
File Size Uploaded Provenance Actions
python_hiccup-1.1.0.tar.gz 22.0 kB Without provenance Details for python_hiccup-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-hiccup 1.1.0
File Interpreter ABI Platform Actions
python_hiccup-1.1.0-py3-none-any.whl Python 3 none any Details for python_hiccup-1.1.0-py3-none-any.whl

Total release size: 30.7 kB

Release files / python_hiccup-1.1.0.tar.gz

Download URL python_hiccup-1.1.0.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d4d709644a87ee0dd6b40b4cee6ad4b8fc3da6241c0814625eb3eadebe73aca8
BLAKE2b-256 checksum
How to use checksums
046ddfa844bfffefac14a8f4bd27ae94ad998e0a1bba975c0c53481fd0b1dcc9
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.1.0-py3-none-any.whl

Download URL python_hiccup-1.1.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
638271828f701bde5d46f3706d158b67970d4dfadf2ac971bc593fa56b1675d5
BLAKE2b-256 checksum
How to use checksums
d02d9b7016ff97fe90b82c156ff00494641ef3e867ab0c474d7d7aa3816efafe
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.1.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.

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