asgi-htmx
HTMX integration for ASGI applications. Works with Starlette, FastAPI, Quart -- or any other web framework supporting ASGI that exposes the ASGI scope. Inspired by django-htmx.
Table of contents
Installation
NOTE: This is alpha software. Please be sure to pin your dependencies.
pip install asgi-htmx==0.1.*
Quickstart
First, ensure HTMX is installed.
For example, download a copy of htmx.min.js, add it to your static files, then add the script tag to templates:
<script src="{{ url_for('static', path='/js/htmx.min.js') }}" defer></script>
Now, install HtmxMiddleware onto the ASGI app:
-
Using Starlette:
from asgi_htmx import HtmxMiddleware from starlette.middleware import Middleware app = Starlette( middleware=[ ..., Middleware(HtmxMiddleware), ..., ], )
-
Using FastAPI:
from asgi_htmx import HtmxMiddleware from fastapi import FastAPI app = FastAPI() app.add_middleware(HtmxMiddleware)
You can now access scope["htmx"] (an instance of HtmxDetails) in endpoints:
# `HtmxRequest` makes code editors type-check `request.scope["htmx"]`
from asgi_htmx import HtmxRequest as Request
from .resources import templates
async def home(request: Request):
template = "home.html"
context = {"request": request}
if (htmx := request.scope["htmx"]):
template = "partials/items.html"
context["boosted"] = htmx.boosted # ...
return templates.TemplateResponse(template, context)
See examples for full working example code.
API Reference
HtmxMiddleware
An ASGI middleware that sets scope["htmx"] to an instance of HtmxDetails (scope refers to the ASGI scope).
app = HtmxMiddleware(app)
HtmxDetails
A helper that provides shortcuts for accessing HTMX-specific request headers.
htmx = HtmxDetails(scope)
__bool__() -> bool- ReturnTrueif the request was made using HTMX (HX-Requestis present),Falseotherwise.boosted: bool- Mirrors theHX-Boostedheader:Trueif the request is via an element with thehx-boostattribute.current_url: str | None- Mirrors theHX-Current-URLheader: The current URL of the browser, orNonefor non-HTMX requests.history_restore_request: str- Mirrors theHX-History-Restore-Requestheader:Trueif the request is for history restoration after a miss in the local history cache.prompt: str | None- MirrorsHX-Prompt: The user response tohx-promptif it was used, orNone.target: str | None- MirrorsHX-Target: Theidof the target element if it exists, orNone.trigger: str | None- MirrorsHX-Trigger: Theidof the trigger element if it exists, orNone.trigger_name: str | None- MirrorsHX-Trigger-Name: Thenameof the trigger element if it exists, orNone.triggering_event: Any | None- MirrorsTriggering-Event, which is set by the event-header extension: The deserialized JSON representation of the event that triggered the request if it exists, orNone.
HtmxRequest
For Starlette-based frameworks, use this instead of the standard starlette.requests.Request so that code editors understand that request.scope["htmx"] contains an HtmxDetails instance:
from asgi_htmx import HtmxRequest as Request
async def home(request: Request):
reveal_type(request.scope["htmx"]) # Revealed type is 'HtmxDetails'
License
MIT
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog.
0.1.0 - 2022-05-30
Initial release
Added
- Add
HtmxDetailsthat facilitates working withHX-*request headers. - Add
HtmxMiddlewarethat automatically adds anHtmxDetailsinstance asscope["htmx"]. - Add
HtmxRequestthat facilitates type hintingrequest.scope["htmx"]in Starlette-based frameworks.
Metadata
Release files for asgi-htmx 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asgi-htmx-0.1.0.tar.gz | 24.9 kB | Without provenance | Details for asgi-htmx-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asgi_htmx-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for asgi_htmx-0.1.0-py3-none-any.whl |
Total release size: 31.3 kB
Release files / asgi-htmx-0.1.0.tar.gz
| Download URL | asgi-htmx-0.1.0.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c6cb1419f05d53ea593bfe684c88b4941b22063c7628720ec99de9197587623
|
|
BLAKE2b-256 checksum How to use checksums |
ab6bd013c1e0fd6de4f8fbbc253d74766df50146bacfee856093722f10d30a2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.4
|
Release files / asgi_htmx-0.1.0-py3-none-any.whl
| Download URL | asgi_htmx-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
622a98b5c31a8b9857de6a5a9cd97d584b0ee21b88e0a8a56dc237d172b75c9b
|
|
BLAKE2b-256 checksum How to use checksums |
3bc75490c81826f003038fab6047384ac77fd848296453e6dd0784b055ebba41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.4
|
Security for asgi-htmx 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.