larzhtml
Build HTML in Python - safely. Pure Python, zero dependencies.
Composing HTML with f-strings is how XSS holes get made. larzhtml lets you build
markup as nested Python calls where every text child and attribute value is
HTML-escaped automatically; you opt out explicitly with raw() for content you
already trust.
from larzhtml import div, p, a, ul, li, raw
div(
p("Hello ", a("world", href="https://x.com")),
ul(*[li(x) for x in items]),
class_="card", id="main",
)
# <div class="card" id="main"><p>Hello <a href="https://x.com">world</a></p>...</div>
div("<script>alert(1)</script>") # -> <div><script>...</script></div>
Why
- Safe by construction. Text and attribute values are escaped by default, so
untrusted data can't inject tags or break out of an attribute. The XSS-prone
thing (
raw(...)) is the one you have to ask for. - Just Python. Elements are function calls; compose with loops, comprehensions,
and conditionals - no template language, no
{% %}. Great for emails, snippets, and server-rendered fragments. - Ergonomic attributes.
class_/for_(trailing underscore),data_toggle->data-toggle, boolean attrs (disabled=True->disabled),None/Falseomitted. - Zero dependencies. No
lxml, nodominate, nomarkupsafe.
Install
pip install larzhtml
Usage
from larzhtml import div, p, a, img, ul, li, tag, raw, document
div(p("safe ", raw("<b>trusted</b>")), class_="box")
img(src="/logo.png", alt="Logo") # void: <img ...>
tag("my-widget")("content", data_id="1") # any element
document(div("body"), title="Home", lang="en") # full HTML5 doc
Tests
python -m unittest discover -s tests -v # 18 tests incl. XSS escaping
The Larz stack
One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter - pairs with larzmark and larztemplate.
License
MIT (c) larz-scripter
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 larzhtml-0.1.0.tar.gz.
File metadata
- Download URL: larzhtml-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ff44baeb07caa6a4e3f008c40e1dfedeb96cff91fc18218089c5b8ed38ef23d
|
|
| MD5 |
3963c22f1c0bf34cd4552803b7694cad
|
|
| BLAKE2b-256 |
425270a39191a6b685c3b758b9696d784ca2362eeafaca8e2eb36cdf18e59057
|
File details
Details for the file larzhtml-0.1.0-py3-none-any.whl.
File metadata
- Download URL: larzhtml-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66cae190d9e72d0f04867fcbce511ed264316afb2c836faad4c4b7b61ceff301
|
|
| MD5 |
ec33dfe90f9116d0e52b8139846a3653
|
|
| BLAKE2b-256 |
f6155734b819637f80b2ba087a9bc2eebd5d579b126f378dcdcab74cac77d69d
|