Skip to main content
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 https://static.pepy.tech/personalized-badge/flask-htmx?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week https://static.pepy.tech/personalized-badge/flask-htmx?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month https://static.pepy.tech/personalized-badge/flask-htmx?period=total&units=international_system&left_color=grey&right_color=blue&left_text=downloads

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, render_template
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

Release files for flask-htmx 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-htmx 0.4.0
File Size Uploaded
flask_htmx-0.4.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-htmx 0.4.0
File Interpreter ABI Platform
flask_htmx-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / flask_htmx-0.4.0.tar.gz

Download URL flask_htmx-0.4.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2d367fb27c8da99d031a0c566b7e562637139722e2d4e8ec67c7f941addb22fd
BLAKE2b-256 checksum
How to use checksums
4ab71ba8b722ccc12b72b44af949f438a85111ba8db9e39f973dff4a47da068e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.9.20 Linux/6.8.0-1014-azure

Release files / flask_htmx-0.4.0-py3-none-any.whl

Download URL flask_htmx-0.4.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac0ef976638bc635537a47c4ae622c91aef1e69d8bf52880aa9ae0db089ce7d2
BLAKE2b-256 checksum
How to use checksums
3d8e7e75c2210567ba11df9ea7d031eb5b8f45e82f6112cc8be885cb0ce86c7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.9.20 Linux/6.8.0-1014-azure

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page