Skip to main content

htmx-Flask

htmx-Flask is an extension for Flask that adds support for htmx to your application. It simplifies using htmx with Flask by enhancing the global request object and providing a new make_response function.

Install

It's just pip install htmx-flask and you're all set. It's a pure Python package that only needs flask (for obvious reasons!).

Usage

Htmx Request

Before using the enhanced request, you need to initialize the extension with:

from flask import Flask
from htmx_flask import Htmx

htmx = Htmx()

app = Flask(__name__)
htmx.init_app(app)

After that, you can use htmx_flask.request.htmx to easily access htmx request headers. For example, instead of:

from flask import request
from my_app import app

@app.route("/")
def hello_workd():
    if request.headers.get("HX-Request") == "true":
        is_boosted = "Yes!" if request.headers.get("HX-Boosted") == "true" else "No!"
        current_url = request.headers.get("HX-Current-URL")
        return (
            "<p>Hello World triggered from a htmx request.</p>"
            f"<p>Boosted: {is_boosted}</p>"
            f"<p>The current url is {current_url}."
        )
    else:
        return "<p>Hello World triggered from a regular request.</p>"

You can do:

from htmx_flask import request
from my_app import app

@app.route("/")
def hello_workd():
    if request.htmx:
        is_boosted = "Yes!" if request.htmx.boosted else "No!"
        current_url = request.htmx.current_url
        return (
            "<p>Hello World triggered from a htmx request.</p>"
            f"<p>Boosted: {is_boosted}</p>"
            f"<p>The current url is {current_url}."
        )
    else:
        return "<p>Hello World triggered from a regular request.</p>"

Htmx response

You might be interested on adding htmx response headers to your response. Use htmx_flask.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 htmx_flask 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"},
    )

IntelliSense

By using htmx-Flask you will also get the benefits of code completion, parameter info and quick info on your IDE. Check out these screenshots from PyCharm:

request.htmx autocomplete

make_response quick info

make_response parameter info

How to contribute

This project uses pre-commit hooks to run black, isort, pyupgrade and flake8 on each commit. To have that running automatically on your environment, install the project with:

pip install -e .[dev]

And then run once:

pre-commit install

From now one, every time you commit your files on this project, they will be automatically processed by the tools listed above.

How to run tests

You can install pytest and other required dependencies with:

pip install -e .[tests]

And then run the test suite with:

pytest

Metadata

Release files for htmx-flask 0.1.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 htmx-flask 0.1.0
File Size Uploaded Provenance Actions
htmx_flask-0.1.0.tar.gz 126.1 kB Without provenance Details for htmx_flask-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 133.9 kB

Release files / htmx_flask-0.1.0.tar.gz

Download URL htmx_flask-0.1.0.tar.gz
Size 126.1 kB
Tags Source
SHA-256 checksum
How to use checksums
393defc4a5a20a006fc4a96254f6a3ee8522789e7d886d22a02486ecb9d176af
BLAKE2b-256 checksum
How to use checksums
5ebbd522908000e391109cf168e1d0fe8c09b0f1173571dcf2bbcb1ca94a085c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / htmx_flask-0.1.0-py3-none-any.whl

Download URL htmx_flask-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c50833c8ac0686182ac9a93d7b90b6c987753c7f3b02a1641ccdfdb19126c4d4
BLAKE2b-256 checksum
How to use checksums
c3e6dddf9f27231c8bfb39c8c287d2017710189de235e662e463bf4cb4e9c5fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Security for htmx-flask 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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