Skip to main content

Python Hiccup

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

CircleCI

CodeScene 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 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 &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>

Resources

Existing python alternatives

Development

Running lint:

uv run ruff check

Running type checks:

uv run ty check

Running tests:

uv run pytest

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_hiccup-0.4.1.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_hiccup-0.4.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file python_hiccup-0.4.1.tar.gz.

File metadata

  • Download URL: python_hiccup-0.4.1.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.0

File hashes

Hashes for python_hiccup-0.4.1.tar.gz
Algorithm Hash digest
SHA256 690d4eebedf17f9f886bbe0b8b291720a2a30edf7406b2ff31bb91ec9a28c689
MD5 ec70b028fdb94705b4ebad753cc52d2c
BLAKE2b-256 c507f0425a923abbc64744da1ae3abf4c01f0d53585e95140c7e4c6a950e1459

See more details on using hashes here.

File details

Details for the file python_hiccup-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: python_hiccup-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.0

File hashes

Hashes for python_hiccup-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b6949ed8ca90d4103a39fd2f487b2a5ca4d984bc0701661359a9977459d92bca
MD5 bd71f7ae8f53de047955441f871625ab
BLAKE2b-256 d2b90566c3fff5211dbc99311274cc68ec30a19995136a3b6d390f0518452521

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page