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.
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 ©.
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>
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 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15fdfa062a45b57238f3d69626f2179d08adb864114317c6fa1b01257eeebf85
|
|
| MD5 |
a1808c798618fb3faa869021da4d4d08
|
|
| BLAKE2b-256 |
3a7c4c3bedb2b4a2be54b5ae4073bd232360d4a60d85cc1a66a31331f16e9a9a
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c817351f01040a4d1a2be49e2aec62221d7db583f495d2f7d87329a497d4f66
|
|
| MD5 |
a58610da86ac91dccd3258f93f36c8ef
|
|
| BLAKE2b-256 |
fa05628c7bc3cca816ca1a8473a2c8940d24525618ca8ef9b92e89d5ab99fbe7
|