Skip to main content

htmx support for Flask

Project description

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

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

htmx_flask-0.1.0.tar.gz (126.1 kB view details)

Uploaded Source

Built Distribution

htmx_flask-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file htmx_flask-0.1.0.tar.gz.

File metadata

  • Download URL: htmx_flask-0.1.0.tar.gz
  • Upload date:
  • Size: 126.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for htmx_flask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 393defc4a5a20a006fc4a96254f6a3ee8522789e7d886d22a02486ecb9d176af
MD5 e9ddfce465a59a39198d9306e384b156
BLAKE2b-256 5ebbd522908000e391109cf168e1d0fe8c09b0f1173571dcf2bbcb1ca94a085c

See more details on using hashes here.

File details

Details for the file htmx_flask-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: htmx_flask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for htmx_flask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c50833c8ac0686182ac9a93d7b90b6c987753c7f3b02a1641ccdfdb19126c4d4
MD5 fa153f7458901ea9c94a09eb9dc0b9d7
BLAKE2b-256 c3e6dddf9f27231c8bfb39c8c287d2017710189de235e662e463bf4cb4e9c5fd

See more details on using hashes here.

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