{"id":13780553,"url":"https://github.com/florimondmanca/asgi-htmx","last_synced_at":"2025-10-05T09:51:14.872Z","repository":{"id":39618912,"uuid":"497433418","full_name":"florimondmanca/asgi-htmx","owner":"florimondmanca","description":"HTMX integration for ASGI applications","archived":false,"fork":false,"pushed_at":"2023-05-29T14:00:19.000Z","size":41,"stargazers_count":67,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-11T17:53:36.602Z","etag":null,"topics":["asgi","fastapi","htmx","python","starlette"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/florimondmanca.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-05-28T21:44:06.000Z","updated_at":"2024-12-31T02:25:35.000Z","dependencies_parsed_at":"2024-01-07T00:07:23.160Z","dependency_job_id":null,"html_url":"https://github.com/florimondmanca/asgi-htmx","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/florimondmanca%2Fasgi-htmx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/florimondmanca%2Fasgi-htmx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/florimondmanca%2Fasgi-htmx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/florimondmanca%2Fasgi-htmx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/florimondmanca","download_url":"https://codeload.github.com/florimondmanca/asgi-htmx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233686180,"owners_count":18714103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","fastapi","htmx","python","starlette"],"created_at":"2024-08-03T18:01:17.148Z","updated_at":"2025-09-20T20:31:35.128Z","avatar_url":"https://github.com/florimondmanca.png","language":"Python","funding_links":[],"categories":["Third Party Packages 📦 \u003ca name = \"tools\"\u003e\u003c/a\u003e"],"sub_categories":["Helper Libraries"],"readme":"# asgi-htmx\n\n[![Build Status](https://dev.azure.com/florimondmanca/public/_apis/build/status/florimondmanca.asgi-htmx?branchName=master)](https://dev.azure.com/florimondmanca/public/_build?definitionId=18)\n[![Coverage](https://codecov.io/gh/florimondmanca/asgi-htmx/branch/master/graph/badge.svg)](https://codecov.io/gh/florimondmanca/asgi-htmx)\n[![Package version](https://badge.fury.io/py/asgi-htmx.svg)](https://pypi.org/project/asgi-htmx)\n\n[HTMX](https://htmx.org) integration for [ASGI](https://asgi.readthedocs.io/en/latest/) applications. Works with Starlette, FastAPI, Quart -- or any other web framework supporting ASGI that exposes the ASGI `scope`. Inspired by [django-htmx](https://github.com/adamchainz/django-htmx).\n\n**Table of contents**\n\n- [Installation](#installation)\n- [Quickstart](#quickstart)\n- [API Reference](#api-reference)\n\n## Installation\n\n**NOTE**: This is alpha software. Please be sure to pin your dependencies.\n\n```\npip install asgi-htmx==0.1.*\n```\n\n## Quickstart\n\nFirst, ensure [HTMX is installed](https://htmx.org/docs/#installing).\n\nFor example, download a copy of `htmx.min.js`, add it to your [static files](https://www.starlette.io/staticfiles/), then add the script tag to templates:\n\n```html\n\u003cscript src=\"{{ url_for('static', path='/js/htmx.min.js') }}\" defer\u003e\u003c/script\u003e\n```\n\nNow, install `HtmxMiddleware` onto the ASGI app:\n\n* Using Starlette:\n\n    ```python\n    from asgi_htmx import HtmxMiddleware\n    from starlette.middleware import Middleware\n\n    app = Starlette(\n        middleware=[\n            ...,\n            Middleware(HtmxMiddleware),\n            ...,\n        ],\n    )\n\n* Using FastAPI:\n\n    ```python\n    from asgi_htmx import HtmxMiddleware\n    from fastapi import FastAPI\n\n    app = FastAPI()\n    app.add_middleware(HtmxMiddleware)\n    ```\n\nYou can now access `scope[\"htmx\"]` (an instance of [`HtmxDetails`](#htmxdetails)) in endpoints:\n\n```python\n# `HtmxRequest` makes code editors type-check `request.scope[\"htmx\"]`\nfrom asgi_htmx import HtmxRequest as Request\n\nfrom .resources import templates\n\nasync def home(request: Request):\n    template = \"home.html\"\n    context = {\"request\": request}\n\n    if (htmx := request.scope[\"htmx\"]):\n        template = \"partials/items.html\"\n        context[\"boosted\"] = htmx.boosted  # ...\n\n    return templates.TemplateResponse(template, context)\n```\n\nSee [examples](./examples) for full working example code.\n\n## API Reference\n\n### `HtmxMiddleware`\n\nAn ASGI middleware that sets `scope[\"htmx\"]` to an instance of [`HtmxDetails`](#htmxdetails) (`scope` refers to the ASGI scope).\n\n```python\napp = HtmxMiddleware(app)\n```\n\n### `HtmxDetails`\n\nA helper that provides shortcuts for accessing HTMX-specific [request headers](https://htmx.org/reference/#request_headers).\n\n```python\nhtmx = HtmxDetails(scope)\n```\n\n* `__bool__() -\u003e bool` - Return `True` if the request was made using HTMX (`HX-Request` is present), `False` otherwise.\n* `boosted: bool` - Mirrors the `HX-Boosted` header: `True` if the request is via an element with the [`hx-boost`](https://htmx.org/attributes/hx-boost/) attribute.\n* `current_url: str | None` - Mirrors the `HX-Current-URL` header: The current URL of the browser, or `None` for non-HTMX requests.\n* `history_restore_request: str` - Mirrors the `HX-History-Restore-Request` header: `True` if the request is for history restoration after a miss in the local history cache.\n* `prompt: str | None` - Mirrors `HX-Prompt`: The user response to [`hx-prompt`](https://htmx.org/attributes/hx-prompt/) if it was used, or `None`.\n* `target: str | None` - Mirrors `HX-Target`: The `id` of the target element if it exists, or `None`.\n* `trigger: str | None` - Mirrors `HX-Trigger`: The `id` of the trigger element if it exists, or `None`.\n* `trigger_name: str | None` - Mirrors `HX-Trigger-Name`: The `name` of the trigger element if it exists, or `None`.\n* `triggering_event: Any | None` - Mirrors `Triggering-Event`, which is set by the [event-header extension](https://htmx.org/extensions/event-header/): The deserialized JSON representation of the event that triggered the request if it exists, or `None`.\n\n### `HtmxRequest`\n\nFor 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:\n\n```python\nfrom asgi_htmx import HtmxRequest as Request\n\nasync def home(request: Request):\n    reveal_type(request.scope[\"htmx\"])  # Revealed type is 'HtmxDetails'\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflorimondmanca%2Fasgi-htmx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflorimondmanca%2Fasgi-htmx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflorimondmanca%2Fasgi-htmx/lists"}