Skip to main content

HTML/SVG generation for Python. Zero dependencies.

Project description

html-tags

PyPI version

[!WARNING] Under active development — Mar 2026

Concise, immutable HTML/SVG generation for Python. Zero dependencies. Purely functional — data and rendering are fully separated.

from html_tags import setup_tags, setup_svg, to_html

setup_tags(); setup_svg()

page = Html(
    Head(Title("hello")),
    Body(
        H1("hello world", cls="title"),
        Svg(Circle(cx="50", cy="50", r="40")),
    )
)

print(to_html(page))

Core design

Tags are inert dataTag is a namedtuple describing structure. Rendering is a function that operates on it:

  • to_html(tag) — render to HTML string
  • pretty(tag) — indented HTML for debugging
  • validate(tag) — check structural rules
  • HTML(tag) — thin wrapper for notebook display
>>> Div(P("hello"), cls="main")
Div(cls='main')(P('hello'))

>>> to_html(Div(P("hello"), cls="main"))
'<div class="main"><p>hello</p></div>'

Immutable construction

Tags are immutable — call to append children or merge attributes:

Div(cls="card")("content", id="main")

SVG elements self-close correctly (<line />) and shapes like Circle accept children for animations:

Circle(cx=50, cy=50, r=20, fill='blue')(
    AnimateTransform(attributeName='transform', type='rotate',
        values='0 50 50;360 50 50', dur='1s', repeatCount='indefinite'))

Parsing HTML

Convert HTML strings back into Tag trees:

>>> html_to_tag('<div id="main"><p class="greeting">hello</p></div>')
Div(id='main')(P(cls='greeting')('hello'))

Raw HTML

Use Safe to pass pre-escaped HTML strings through without escaping:

Safe('<b>already escaped</b>')

Script and Style tags handle raw content natively — no wrapping needed.

Fragments

Group children without a wrapping element:

Fragment(P("one"), P("two"), P("three"))

Gotchas

Keyword attribute names convert _ to -:

Circle(stroke_width="2")  # → stroke-width="2"

Dict keys pass through verbatim — use for special syntax or reserved names:

Div({"data-on:click__once": "@post('/api')"})
FeBlend({"mode": "multiply"})

HTML void elements render without slash (<br>), SVG elements self-close (<line />).

Positional args must come before keyword args (Python rule) — use __call__ chaining to add children after attrs:

Circle(cx=50, cy=50, r=20)(Animate(attributeName='r', values='18;22;18', dur='2s'))

Security

Validates against injection, not HTML structure:

  • <script>/<style> content checked for closing tag injection
  • URL attributes reject javascript: and vbscript: schemes
  • Attribute names validated against injection patterns
  • Void elements reject children, <html> rejects nesting

Structural correctness (e.g. <li> inside <ul>) is left to the browser.

Install

pip install html-tags

License

MIT

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

html_tags-0.1.13.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

html_tags-0.1.13-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file html_tags-0.1.13.tar.gz.

File metadata

  • Download URL: html_tags-0.1.13.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for html_tags-0.1.13.tar.gz
Algorithm Hash digest
SHA256 6abb400d79c0fd192f84da5bc8745847fda2d37bfcaf3d50f44d2cf079d12b52
MD5 8851169e67b2e71fda75f0f40a6ac316
BLAKE2b-256 5a4ab1afb53187230dfb34ed87b0f09eaf6f9c340e8838ab487c9b506f0b86b3

See more details on using hashes here.

File details

Details for the file html_tags-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: html_tags-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for html_tags-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 4d41cdad244e71b6799098dbf32ec0bd98ec66dff51e8e7d7c81540fcd5c00c2
MD5 e044dde1c92430817ceaccf9f18a590b
BLAKE2b-256 e4dc5573eea5190e3ae71caf83a677e7119adbabe877e39e2281f7c4d0c48270

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