An HTMX extension for markupy
Project description
markupy_htmx
Project description
markupy_htmx is an extension for markupy that allows for a simpler HTMX attribute generation by enabling the following:
- Full coverage of HTMX attributes, all rewritten as convenient Python functions/helpers
- Static typing to improve reliability and error detection
- IDE autocomplete of existing attributes and for each of them, parameter types and values
Installation
markupy_htmx can be installed via PYPI:
pip install markupy_htmx
Like its dependency markupy, it is targeting Python 3.10+ versions.
Basic usage
All HTMX attributes are exposed via markupy_htmx.attributes that we recommend you alias to hx in order to call attributes with hx.attr(...)
from markupy.elements import Button
from markupy_htmx import attributes as hx
el = Button(hx.post("/clicked"), hx.swap("outerHTML"))["Click me"]
print(el)
The generated HTML will be the following:
<button hx-post="/clicked" hx-swap="outerHTML">Click Me</button>
Advanced usage
As you saw above, most of the markupy_htmx attributes have exactly the same construct as their "native" counterpart.
Some attributes benefited from small adjustments, mostly to improve developer experience and ensure compatibility with Python.
hx-on:* attribute
As opposed to the HTMX native attribute that expects the event as part of the attribute name, markupy_htmx expects the event as the very first argument of hx.on():
Button(hx.on("htmx:beforeRequest", "alert('Making a request!')"))
<button hx-on:htmx:before-request="alert('Making a request!')"></button>
Several things to note here:
- The event name can be written both in it's original "camelCase" Javascript syntax or the adapted "kebab-case" syntax enforced by HTMX when written as part of the attribute name
- All valid HTMX events and standard HTML events will be autocompleted by your IDE for convenience, but you can as well type your own events
- The Javascript content is escaped by
markupywhen it is rendered as part of an attribute's value, but the code is interpreted normally by the browsers
Multi-value attributes
Some HTMX attributes accept multiple values (either comma or space separated).
To make it more convenient for you to provide these values without having to remember the expected params or value separator, markupy_htmx allows for multiple definitions of the same attribute and will take care of the concatenation for you.
Here's the list of such attributes:
- hx-select-oob
- hx-trigger
- hx-disabled-elt
- hx-ext
- hx-params
- hx-disinherit
- hx-inherit
hx-select-oob
from markupy.elements import Input
from markupy_htmx import attributes as hx
el1 = Input(hx.select_oob("#hello"), hx.select_oob(("#alert", "afterbegin")))
el2 = Input(hx.select_oob("#hello", ("#alert", "afterbegin")))
Both elements el1 and el2are equivalent and will render as:
<input hx-select-oob="#hello, #alert:afterbegin">
hx-trigger
from markupy.elements import Button
from markupy_htmx import attributes as hx
Button(hx.trigger("load"), hx.trigger("click", delay_ms=1000))["Click"]
<button hx-trigger="load, click delay:1000ms">Click</button>
Talking about the hx-trigger attribute, it has a special variant called hx.trigger_every_ms() that is used for polling. It's a dedicated method because it takes different params from the regular "event based" triggers:
Button(hx.trigger_every_ms(1000, filters="myConditional"))
<button hx-trigger="every 1000ms [myConditional]"></button>
Python dict to JSON
Some attributes such as hx-vals, hx-headers or hx-request take JSON objects as arguments. With markupy_htmx, you can write them as dict and they will be properly converted (and escaped):
Button(hx.vals({"myStr": "My Value", "anInt": 3, "aBool": True}))
Boolean attributes
HTMX has a lot of boolean attributes and properties. All of them are mapped to Python bool:
Body(hx.boost(True), hx.push_url(False))
Some boolean attributes can also omit a value and will take a default value:
Div(hx.preserve())
Div(hx.disable())
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
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 markupy_htmx-1.0.1.tar.gz.
File metadata
- Download URL: markupy_htmx-1.0.1.tar.gz
- Upload date:
- Size: 35.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
452704e547b269565e77036cf059ab14ae2763a017c298d3dffc1ef86c07540f
|
|
| MD5 |
c5ad642d89094670a1bcc3b099a71139
|
|
| BLAKE2b-256 |
f461f795d4cf1415ff7623ea7c9143c2f13a3423e4b16d6038ad0f7fc88a6257
|
File details
Details for the file markupy_htmx-1.0.1-py3-none-any.whl.
File metadata
- Download URL: markupy_htmx-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f5f7861f6c7c6bf0f7260870830ded214e624b31e3c7ae290a3668d0cbfe014
|
|
| MD5 |
a910ca788cb6266cd1dc0b1db05e9269
|
|
| BLAKE2b-256 |
087eb15e43fc5eb5451bc616630d183dee18ef28fc12c02c2832d6aff9874a35
|