Flask-HTMX
A Flask extension to work with HTMX.
Documentation: https://flask-htmx.readthedocs.io
Quickstart
Install the extension with pip.
pip install flask-htmx
Or perhaps you use Poetry.
poetry add flask-htmx
HTMX Request
You can register the HTMX object by passing the Flask
app object via the constructor.
htmx = HTMX(app)
Or you can register the HTMX object using HTMX.init_app().
htmx = HTMX()
htmx.init_app(app)
A minimal working example.
from flask import Flask
from flask_htmx import HTMX
app = Flask(__name__)
htmx = HTMX(app)
@app.route("/")
def home():
if htmx:
return render_template("partials/thing.html")
return render_template("index.html")
The above example checks whether the request came
from HTMX or not. If htmx evaluates to
True, then it was a HTMX request, else
False.
This allows you to return a partial HTML when it’s a HTMX request or the full page HTML when it is a normal browser request.
Flask-HTMX also supports checking for HTMX headers during a request in the view. For example, check the current URL of the browser of a HTMX request.
@app.route("/")
def home():
current_url = htmx.current_url
return render_template("index.html", current_url=current_url)
Other HTMX request headers are also available. See https://htmx.org/reference/#request_headers.
HTMX Response
You might be interested on adding
htmx response headers to your response.
Use flask_htmx.make_response for that. For example, instead of:
import json
from flask import make_response
from my_app import app
@app.route("/hola-mundo")
def hola_mundo():
body = "Hola Mundo!"
response = make_response(body)
response.headers["HX-Push-URL"] = "false"
trigger_string = json.dumps({"event1":"A message", "event2":"Another message"})
response.headers["HX-Trigger"] = trigger_string
return response
You can do:
from flask_htmx import make_response
from my_app import app
@app.route("/hola-mundo")
def hola_mundo():
body = "Hola Mundo!"
return make_response(
body,
push_url=False,
trigger={"event1": "A message", "event2": "Another message"},
)
Documentation
Visit the full documentation.
Development
Installation
poetry install
Running tests
poetry run pytest
Coverage
poetry run pytest --cov=flask_htmx tests/
Docs
sphinx-autobuild docs docs/_build/html
Metadata
Release files for flask-htmx-fork 0.2.2
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 |
|---|---|---|---|---|
| flask_htmx_fork-0.2.2.tar.gz | 5.9 kB | Without provenance | Details for flask_htmx_fork-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_htmx_fork-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for flask_htmx_fork-0.2.2-py3-none-any.whl |
Total release size: 12.8 kB
Release files / flask_htmx_fork-0.2.2.tar.gz
| Download URL | flask_htmx_fork-0.2.2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b01fda643f354cb8792310422a50296ead864884c6ad772f57225ade57cc844
|
|
BLAKE2b-256 checksum How to use checksums |
401d6ef9eb85df6b651a6c96d46aa55db09da63905bc1163f8b4dbec0cec0e98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / flask_htmx_fork-0.2.2-py3-none-any.whl
| Download URL | flask_htmx_fork-0.2.2-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0ebbae6416038abf648885cab6489d5bb18f5b554b07cf7e2e9208651c988071
|
|
BLAKE2b-256 checksum How to use checksums |
1324d426c2853d89900e7693d2f1805521c50357af60fb2c9a7639500861bbca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Security for flask-htmx-fork 0.2.2
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.