Package: asgi-htmx
asgi-htmxHTMXintegration forASGIapplications. Works with Starlette, FastAPI, Quart -- or any other web framework supporting ASGI that exposes the ASGIscope. Inspired bydjango-htmx.Table of contentsInstallationQuickstartAPI ReferenceInstallationNOTE: This is alpha software. Please be sure to pin your dependencies.pip install asgi-htmx==0.1.*QuickstartFirst, ensureHTMX is installed.For example, download a copy ofhtmx.min.js, add it to yourstatic files, then add the script tag to templates:<scriptsrc="{{ url_for('static', path='/js/htmx.min.js') }}"defer></script>Now, installHtmxMiddlewareonto the ASGI app:Using Starlette:fromasgi_htmximportHtmxMiddlewarefromstarlette.middlewareimportMiddlewareapp=Starlette(middleware=[...,Middleware(HtmxMiddleware),...,],)Using FastAPI:fromasgi_htmximportHtmxMiddlewarefromfastapiimportFastAPIapp=FastAPI()app.add_middleware(HtmxMiddleware)You can now accessscope["htmx"](an instance ofHtmxDetails) in endpoints:# `HtmxRequest` makes code editors type-check `request.scope["htmx"]`fromasgi_htmximportHtmxRequestasRequestfrom.resourcesimporttemplatesasyncdefhome(request:Request):template="home.html"context={"request":request}if(htmx:=request.scope["htmx"]):template="partials/items.html"context["boosted"]=htmx.boosted# ...returntemplates.TemplateResponse(template,context)Seeexamplesfor full working example code.API ReferenceHtmxMiddlewareAn ASGI middleware that setsscope["htmx"]to an instance ofHtmxDetails(scoperefers to the ASGI scope).app=HtmxMiddleware(app)HtmxDetailsA helper that provides shortcuts for accessing HTMX-specificrequest 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 theevent-header extension: The deserialized JSON representation of the event that triggered the request if it exists, orNone.HtmxRequestFor Starlette-based frameworks, use this instead of the standardstarlette.requests.Requestso that code editors understand thatrequest.scope["htmx"]contains anHtmxDetailsinstance:fromasgi_htmximportHtmxRequestasRequestasyncdefhome(request:Request):reveal_type(request.scope["htmx"])# Revealed type is 'HtmxDetails'LicenseMITChangelogAll notable changes to this project will be documented in this file.The format is based onKeep a Changelog.0.1.0 - 2022-05-30Initial releaseAddedAddHtmxDetailsthat facilitates working withHX-*request headers.AddHtmxMiddlewarethat automatically adds anHtmxDetailsinstance asscope["htmx"].AddHtmxRequestthat facilitates type hintingrequest.scope["htmx"]in Starlette-based frameworks.