Skip to main content

PyHTMX

PyHTMX is a dead simple library to generate HTMX code in Python.

Inspired by this StackOverflow thread on how to generate HTML with XML.

Features

  • Outputs indented code
  • Outputs subtrees for the generated DOM
  • Children elements and content can be provided as lists
  • Building based on nested constructors or with statements

Installing

  $ python3 -m pip install pyhtmx-lib

Examples

First, an example based on nesting constructors:

  from pyhtmx import *

  html = Html(
    [
      Head(
        [
          Title('My Website'),
          Script(src="https://unpkg.com/htmx.org@2.0.3"),
        ]
      ),
      Body(
        [
          Header(
            [
              Img(src='/path/to/logo.png', _id="logo"),
              Nav(
                Ul(
                  [
                    Li(
                      "First item.",
                      hx_post="/first-item-clicked",
                      hx_trigger="click",
                      hx_target="#container-div",
                      hx_swap="innerHTML",
                    ),
                    Li("Second item."),
                    Li("Third item.")
                  ]
                ),
              ),
            ]
          ),
          Div(
            'Content here',
            _id="container-div",
          ),
          Footer([Hr(), 'Copyright 2024']),
        ]
      ),
    ]
  )

html.dump()

The example results in the following output:

<html>
  <head>
    <title>My Website</title>
    <script src="https://unpkg.com/htmx.org@2.0.3" />
  </head>
  <body>
    <header>
      <img src="/path/to/logo.png" id="logo" />
      <nav>
        <ul>
          <li hx-post="/first-item-clicked" hx-trigger="click" hx-target="#container-div" hx-swap="innerHTML">First item.</li>
          <li>Second item.</li>
          <li>Third item.</li>
        </ul>
      </nav>
    </header>
    <div id="container-div">Content here</div>
    <footer>
      <hr />
      <span>Copyright 2024</span>
    </footer>
  </body>
</html>

Now an example based on contexts defined with the with statement,

with Div(_class="btn-primary") as div:
  with A(href="/link/to/another/site"):
    with P("This is the first paragraph.", style="color: blue"):
      pass
  with P("Yet another paragraph.", style="color: red"):
    pass

div.dump()

which results in

<div class="btn-primary">
  <a href="/link/to/another/site">
    <p style="color: blue">This is the first paragraph.</p>
  </a>
  <p style="color: red">Yet another paragraph.</p>
</div>

Related projects

Metadata

Release files for pyhtmx-lib 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 pyhtmx-lib 0.1.0
File Size Uploaded Provenance Actions
pyhtmx_lib-0.1.0.tar.gz 8.5 kB Without provenance Details for pyhtmx_lib-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 16.4 kB

Release files / pyhtmx_lib-0.1.0.tar.gz

Download URL pyhtmx_lib-0.1.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b8460969f8d9b87d8c82ce4e42efc333465fde378326715c1e47ffeb5603732c
BLAKE2b-256 checksum
How to use checksums
2f096358f031ff02eaf232c44748397db26f7a7ca6ed5e6946afaf57d7e6dd9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.2

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

Download URL pyhtmx_lib-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02788827b596ca5d0dd164476ab2ea200ab5a1424e7d31eba78617402cb008d4
BLAKE2b-256 checksum
How to use checksums
0021ff0c425e8a4f6de8288e39ecd7d3a5f0543ad1f9dfea1fca261808e0c6f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.2

Security for pyhtmx-lib 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.

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