Registry / web-framework / flask-htmx

flask-htmx

JSON →
library0.4.0pypypi✓ verified 92d ago

Flask-HTMX is a Flask extension that provides an easy API for working with HTMX from within a Flask request context. It simplifies integrating HTMX into Flask applications by enhancing the global request object and offering a specialized `make_response` function for HTMX-specific headers. The current version is 0.4.0, and the library maintains an active release cadence with regular updates.

pip install flask-htmx
INSTALL
IMPORT
SIG · FLASK-HTMX
F
flask-htmx
web-frameworkpythonv0.4.0
Install
2.2s avg
Import
488ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.507s · 22.5MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 2.2s · import 0.468s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTMX
✓ from flask_htmx import HTMX
✗ from flask_htmx.extension import HTMX
The HTMX class is exposed directly from the top-level package.
make_response
✓ from flask_htmx import make_response
✗ from flask import make_response
Use `flask_htmx.make_response` to easily add HTMX-specific response headers.

This quickstart demonstrates initializing Flask-HTMX and handling both regular and HTMX-triggered requests. It shows how to conditionally render partial content for HTMX requests and how to use `flask_htmx.make_response` to add HTMX-specific response headers, like `HX-Trigger`.

from flask import Flask, render_template from flask_htmx import HTMX app = Flask(__name__) htmx = HTMX(app) @app.route("/") def home(): if htmx: # HTMX request, return a partial template return render_template("partials/thing.html") # Normal browser request, return a full page return render_template("index.html") @app.route("/click-me", methods=["POST"]) def click_me(): # Example of using make_response for HTMX headers from flask_htmx import make_response return make_response("<span>Clicked!</span>", trigger={'myEvent': 'Data'})
Debug
Known issues
breakingOlder versions of `flask-htmx` (pre-0.3.2) may not be compatible with Flask 3.0.0 and later.
fix
Upgrade `flask-htmx` to version 0.3.2 or newer by running `pip install --upgrade flask-htmx` to ensure compatibility with Flask 3.x.
affects: <0.3.2
gotchaUsing `flask.make_response` for responses that require HTMX-specific headers (e.g., `HX-Redirect`, `HX-Push-URL`, `HX-Trigger`) will not correctly apply these headers.
fix
Always import and use `make_response` from `flask_htmx` when generating responses that need HTMX headers. This function provides extended arguments for common HTMX response headers.
affects: All versions
gotchaBefore version 0.4.0, the `htmx` object was not automatically available as a global context variable in Jinja2 templates. You had to explicitly pass it to `render_template`.
fix
For versions prior to 0.4.0, ensure you pass `htmx=htmx` in your `render_template` call (e.g., `render_template('index.html', htmx=htmx)`). In 0.4.0 and later, it's available globally as `htmx`.
affects: <0.4.0
gotchaWhen submitting forms via HTMX POST requests, especially if CSRF protection is enabled in Flask (e.g., with Flask-WTF), you must ensure the CSRF token is included in the HTMX request.
fix
Include the CSRF token in your form or request data. For example, if using Flask-WTF, ensure your form includes `{{ form.csrf_token }}` or pass the token in hidden inputs for HTMX requests.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flask_htmx'
The `flask-htmx` package has not been installed in your Python environment or the virtual environment is not active.
fix
Install the library using pip: `pip install flask-htmx`.
RuntimeError: The HTMX extension is not registered to the current application.
You are attempting to access or use the `htmx` object within a Flask request context before it has been properly initialized and registered with your Flask application.
fix
Ensure you have initialized the extension either by passing the app object directly: `htmx = HTMX(app)`, or by calling `init_app()`: `htmx = HTMX(); htmx.init_app(app)`.
HTMX client-side headers (e.g., HX-Trigger, HX-Location) are not being sent or processed correctly by the browser, despite setting them in Flask.
You are likely using `flask.make_response` instead of `flask_htmx.make_response` to construct your responses. The standard `flask.make_response` does not provide the utility to automatically set HTMX-specific headers.
fix
Replace `from flask import make_response` with `from flask_htmx import make_response` and use the provided keyword arguments for HTMX headers (e.g., `return make_response('content', trigger={'event': 'data'})`).
Upgrade
Version history
0.4.0latest on PyPI · released Sep 22, 2024
Audit
Dependencies
FlaskrequiredCore web framework that this extension builds upon. Flask-HTMX versions 0.3.2 and later support Flask >= 3.0.0.
Agent activity
4 hits · last 30 days
node
4
Resources
flask-htmx — pip install flask-htmx · libregistry