Skip to main content

A Flask extension to work with HTMX.

Project description

https://badge.fury.io/py/flask-htmx.svg Documentation Status https://codecov.io/gh/edmondchuc/flask-htmx/branch/main/graph/badge.svg?token=K6YB3PB33T https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/License-MIT-red.svg

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

Running tests

poetry run pytest

Coverage

poetry run pytest --cov=flask_htmx tests/

Docs

sphinx-autobuild docs docs/_build/html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-htmx-0.1.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

flask_htmx-0.1.4-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-htmx-0.1.4.tar.gz.

File metadata

  • Download URL: flask-htmx-0.1.4.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

Hashes for flask-htmx-0.1.4.tar.gz
Algorithm Hash digest
SHA256 96c1aefbc43fedf822d4a9a6239414071c61bc36bfa7713e95873189a1826547
MD5 ec102b019d1d58205c67b992c4b57116
BLAKE2b-256 3927aaf10326710a21c7368d85eae7546356905e83573e5b938a396e52786dbc

See more details on using hashes here.

Provenance

File details

Details for the file flask_htmx-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: flask_htmx-0.1.4-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

Hashes for flask_htmx-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1ec0e4d88f23fbd9f0a3aaaa9501560bc000b48cdd1fc73eba5e9775454310c1
MD5 171516cd217adac7381165cfb5af27e9
BLAKE2b-256 7ea7113f75a5dc5a5b9f7be935d9512b55c4b8038372ce08ff978801fbe0abc8

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page