Skip to main content

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(&#39;Making a request!&#39;)"></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 markupy when 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

markupy_htmx-1.0.2.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

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

markupy_htmx-1.0.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file markupy_htmx-1.0.2.tar.gz.

File metadata

  • Download URL: markupy_htmx-1.0.2.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for markupy_htmx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 58257505695e45a514b1332028dc6baf79bb1df71deb98492e37d8b2484a27a2
MD5 3ee52adcee6c28da89dccd45f3788d61
BLAKE2b-256 69dfedab5276cc7a81e1352093dfcee8aba8fe2d7f2beb3f88815adc8a91c15a

See more details on using hashes here.

File details

Details for the file markupy_htmx-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: markupy_htmx-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for markupy_htmx-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 14a47499b031b672142bf84003f3f5e3eaea887af548b49c83a44c39453abcf2
MD5 760abfd4fa2ff2633e9512b083542fc6
BLAKE2b-256 aa4839c91e53ff45e84f0671d26baef895642df43d5d03935b25a61a1ea247ac

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