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.0.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.0.0
File Size Uploaded Provenance Actions
python_hiccup-1.0.0.tar.gz 21.7 kB Without provenance Details for python_hiccup-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 30.2 kB

Release files / python_hiccup-1.0.0.tar.gz

Download URL python_hiccup-1.0.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ca33081aa39dd2fffa617c86bbcba8486312d608764a278e860acf1dbc254fcc
BLAKE2b-256 checksum
How to use checksums
ff6594557fd17129865abebc4077a9aa2843439ab0374b66e2da515020d80155
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.0.0-py3-none-any.whl

Download URL python_hiccup-1.0.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f71a95435e63228e25c058df2f9b1dc8de53968a256caeaf234efe448c43eb70
BLAKE2b-256 checksum
How to use checksums
75bdf639e82d77041bc8ab0dc391fda6d44e87eff518ac8050d23831f81e0e01
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.0.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