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
File details
Details for the file flask-htmx-0.1.3.tar.gz
.
File metadata
- Download URL: flask-htmx-0.1.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad7c3154ca7ddaba4af48df137183a5c10da5c3153728d9afda92b461a26f9b5 |
|
MD5 | d705a2c4d8412fcdeb5ee6acf26f4c58 |
|
BLAKE2b-256 | eee80d51252003738ed2d96ef7314194c0f2afec6e128189553ba53740af81ac |
Provenance
File details
Details for the file flask_htmx-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: flask_htmx-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4788ac215c8672524fdd315102d0a9bb8a4f73e851c1de4788b0b21afd708ced |
|
MD5 | 558f6bbd776ed2b68aa20fe2c33ad077 |
|
BLAKE2b-256 | 6fa09b17b0d21fee29d08f6b475a62e8a7950ed9fb8b9bb7afe31ecb0c6645c4 |