Skip to main content

A simple wrapper/generator for HTMX pages

Project description

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

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'),
                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" />
        <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>
      <p style="color: red">Yet another paragraph.</p>
    </a>
  </div>

Related projects

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

pyhtmx_lib-0.0.1.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.

pyhtmx_lib-0.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pyhtmx_lib-0.0.1.tar.gz.

File metadata

  • Download URL: pyhtmx_lib-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.10

File hashes

Hashes for pyhtmx_lib-0.0.1.tar.gz
Algorithm Hash digest
SHA256 afe6bae6fbeb4df5f7d4e3d0eb5d1663a371c045ce299e51f8991751a4934875
MD5 95c45f1f1c1f0230888ff90da221c32f
BLAKE2b-256 7383bd335225b956d2e33ce3c7a10dbdc0b050273e18ab4d46e3bdeb18a6b94b

See more details on using hashes here.

File details

Details for the file pyhtmx_lib-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyhtmx_lib-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.10

File hashes

Hashes for pyhtmx_lib-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13ade533c8f7615fd2f0b71ecb8640f58bdf6cd2ac69dcb1b4af1c52f4bbf9ca
MD5 a67fe4063d9c94bd269fed988ba2bfca
BLAKE2b-256 dec9ffb1e548b8360e869d52034bd35e89b4cb6e8721daab89aeb3a248048623

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