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-htmxVerified import paths — ran on the pinned version, not inferred.
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`.
Upgrade `flask-htmx` to version 0.3.2 or newer by running `pip install --upgrade flask-htmx` to ensure compatibility with Flask 3.x.
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.
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`.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.Install the library using pip: `pip install flask-htmx`.
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)`.
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'})`).