Skip to main content

HTMX integration for pyhtml - build dynamic HTML with HTMX attributes

Project description

PyHTML-HTMX HTMX-Badge

HTMX integration for PyHTML - to build dynamic, hypermedia-driven web applications with only Python, Flask, optionally WebComponents and absolutely no Javascript.

Features

  • Type-safe with full editor autocomplete
  • All 35 HTMX attributes listed from HTMX's web-type document
  • Literal types for (the one set of) enum values (e.g.: swap="innerHTML")
  • Comprehensive documentation in docstrings also from the web-types.
  • Works well with Flask and pyHtml see snippet.

Installation

From GitHub

pip install git+https://github.com/YlanAllouche/pyhtml-htmx.git

Local Development

git clone https://github.com/YlanAllouche/pyhtml-htmx.git
cd pyhtml-htmx
pip install -e . # --break-system-packages

Usage

The htmx() Helper Function

The hx() function creates HTMX attributes with a clean, intuitive API. Note that you use the attribute names without the hx_ prefix inside the function since it's already the name of the function.

Type Safety

The htmx() function provides full type hints with Literal types for (the one set of) enumerated values:

# 'swap' parameter uses Literal type - editor will suggest valid values
**hx(
    swap="innerHTML"    # Shown as valid 
    # swap="invalid"    # LSP error in editor
)

Basic Flask example

import pyhtml as p
from pyhtml_htmx import hx

@app.route("/")
def index():
    return p.html(
        p.head(
            p.title("HTMX App"),
            p.script(src="https://unpkg.com/htmx.org@2.0.3")
        ),
        p.body(
            p.button(
                "Click me",
                **hx(post="/clicked", swap="outerHTML")
            )
        )
    )

@app.route("/clicked", methods=["POST"])
def clicked():
    return p.div("Button clicked")

Future Potential Enhancements

  • Direct attribute support: div("content", get="/url")
    • through monkey-patching
    • or upstream integration
  • either by
    • supporting htmx directly
    • or providing a hook for extra universal attributes
      • at which point other libraries like Alpine could also be easily added

External documentation and dependencies


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

pyhtml_htmx-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

pyhtml_htmx-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pyhtml_htmx-0.1.0.tar.gz.

File metadata

  • Download URL: pyhtml_htmx-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyhtml_htmx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 abb8a815a469acca322ecbdfdd0a7f4289cd9be748201b52b561f8249a2fa5ac
MD5 b75c518d116668f7690f1f78f45fae6c
BLAKE2b-256 7923ac33673c39456482d2a6cc4c0c91b4ecf37c6b9f2c5323fb6800208b3a5f

See more details on using hashes here.

File details

Details for the file pyhtml_htmx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyhtml_htmx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyhtml_htmx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecf1035eb942d0881f5a01f0d099cb3cacfd2dd57cb360559d7490751d8246a4
MD5 4ae2d6bca3eb40efa0091e81f09ee7e7
BLAKE2b-256 7e0701a402693dc3d43015521d674e850863f47de0b228c72c81527932091a6f

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