A Flask extension to work with HTMX.
Project description
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
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.
Continue to the next section of the docs, The HTMX Class.
Development
Installation
poetry install
Docs
sphinx-autobuild docs docs/_build/html
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flask_htmx-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a57069a9741e85d2cd7db5050a1abadc90d053c0dca2039148c5d96ed067fe1c |
|
MD5 | 776868f243377969257534f8276945c3 |
|
BLAKE2b-256 | aa7883c6598eb16d58aa5779956460fc62f57c8ba14ec7c55d947d42bc444bd2 |