Skip to main content

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

Project description

Python Hiccup

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

CircleCI

CodeScene Code Health

Quality Gate Status

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 tests:

uv run pytest

Project details


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.0.tar.gz (19.4 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.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for python_hiccup-0.4.0.tar.gz
Algorithm Hash digest
SHA256 15fdfa062a45b57238f3d69626f2179d08adb864114317c6fa1b01257eeebf85
MD5 a1808c798618fb3faa869021da4d4d08
BLAKE2b-256 3a7c4c3bedb2b4a2be54b5ae4073bd232360d4a60d85cc1a66a31331f16e9a9a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for python_hiccup-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c817351f01040a4d1a2be49e2aec62221d7db583f495d2f7d87329a497d4f66
MD5 a58610da86ac91dccd3258f93f36c8ef
BLAKE2b-256 fa05628c7bc3cca816ca1a8473a2c8940d24525618ca8ef9b92e89d5ab99fbe7

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 Pingdom Monitoring Sentry Error logging StatusPage Status page