Skip to main content

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>&lt;script&gt;...&lt;/script&gt;</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/False omitted.
  • Zero dependencies. No lxml, no dominate, no markupsafe.

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

Metadata

Release files for larzhtml 0.1.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 larzhtml 0.1.0
File Size Uploaded Provenance
larzhtml-0.1.0.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for larzhtml 0.1.0
File Interpreter ABI Platform
larzhtml-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / larzhtml-0.1.0.tar.gz

Download URL larzhtml-0.1.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8ff44baeb07caa6a4e3f008c40e1dfedeb96cff91fc18218089c5b8ed38ef23d
BLAKE2b-256 checksum
How to use checksums
425270a39191a6b685c3b758b9696d784ca2362eeafaca8e2eb36cdf18e59057
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / larzhtml-0.1.0-py3-none-any.whl

Download URL larzhtml-0.1.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66cae190d9e72d0f04867fcbce511ed264316afb2c836faad4c4b7b61ceff301
BLAKE2b-256 checksum
How to use checksums
f6155734b819637f80b2ba087a9bc2eebd5d579b126f378dcdcab74cac77d69d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for larzhtml 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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