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
withstatements
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
Project details
Release history Release notifications | RSS feed
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 pyhtmx_lib-0.0.7.tar.gz.
File metadata
- Download URL: pyhtmx_lib-0.0.7.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7cf76c5ce9caac3ef0bca3f5b8d47c0e7abb5c32f5548402001eedb16a8c1dc
|
|
| MD5 |
58e0065086a5d356c0933a20b1aac80a
|
|
| BLAKE2b-256 |
eed6220b0fb326e4a80ca3da3fac4119e3fddc4b79a115ea9928ad8ec50aad60
|
File details
Details for the file pyhtmx_lib-0.0.7-py3-none-any.whl.
File metadata
- Download URL: pyhtmx_lib-0.0.7-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d407b05e7b0420ef25e85952f9f43a1001620d6078c9e73696812a742ae03f2
|
|
| MD5 |
18e121dcca57d09f91be6bcf85db0c25
|
|
| BLAKE2b-256 |
5361a8ee48214178f4a302d1629a24f930b38d8a0fb5308bdb98c2a2d052a371
|