flask-htmx


Nameflask-htmx JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/edmondchuc/flask-htmx
SummaryA Flask extension to work with HTMX.
upload_time2024-09-22 04:14:20
maintainerNone
docs_urlNone
authorEdmond Chuc
requires_python<4.0,>=3.8
licenseMIT
keywords flask htmx python web
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ##########
Flask-HTMX
##########

.. image:: https://badge.fury.io/py/flask-htmx.svg
    :target: https://badge.fury.io/py/flask-htmx

.. image:: https://readthedocs.org/projects/flask-htmx/badge/?version=latest
    :target: https://flask-htmx.readthedocs.io/en/latest/?badge=latest
    :alt: Documentation Status


.. image:: https://codecov.io/gh/edmondchuc/flask-htmx/branch/main/graph/badge.svg?token=K6YB3PB33T
    :target: https://codecov.io/gh/edmondchuc/flask-htmx


.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
    :target: https://github.com/psf/black

.. image:: https://img.shields.io/badge/License-MIT-red.svg
    :target: https://github.com/edmondchuc/flask-htmx/blob/main/LICENSE

.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week
    :target: https://pepy.tech/project/flask-htmx

.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month
    :target: https://pepy.tech/project/flask-htmx

.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=total&units=international_system&left_color=grey&right_color=blue&left_text=downloads
    :target: https://pepy.tech/project/flask-htmx

A Flask extension to work with HTMX.

Documentation: https://flask-htmx.readthedocs.io

.. quickstart-startblock

Quickstart
==========

Install the extension with pip.

.. code-block:: bash

    pip install flask-htmx

Or perhaps you use Poetry.

.. code-block:: bash

    poetry add flask-htmx

HTMX Request
------------

You can register the HTMX object by passing the Flask
:code:`app` object via the constructor.

.. code-block:: python

    htmx = HTMX(app)

Or you can register the HTMX object using
`HTMX.init_app() <https://flask-htmx.readthedocs.io/en/latest/flask_htmx.htmx.html#flask_htmx.htmx.HTMX.init_app>`_.

.. code-block:: python

    htmx = HTMX()
    htmx.init_app(app)

A minimal working example.

.. code-block:: python

    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 :code:`htmx` evaluates to
`True <https://docs.python.org/3/library/constants.html#True>`_, then it was a HTMX request, else
`False <https://docs.python.org/3/library/constants.html#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.

.. code-block:: python

    @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 <https://htmx.org/reference/#response_headers>`_ to your response.
Use :code:`flask_htmx.make_response` for that. For example, instead of:

.. code-block:: python

    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:

.. code-block:: python

    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"},
        )

.. quickstart-endblock

Documentation
=============
Visit the `full documentation <https://flask-htmx.readthedocs.io>`_.

Development
===========

Installation
------------

.. code-block:: bash

    poetry install

Running tests
-------------

.. code-block:: bash

    poetry run pytest

Coverage
--------

.. code-block:: bash

    poetry run pytest --cov=flask_htmx tests/

Docs
----

.. code-block:: bash

    sphinx-autobuild docs docs/_build/html

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/edmondchuc/flask-htmx",
    "name": "flask-htmx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "Flask, HTMX, Python, Web",
    "author": "Edmond Chuc",
    "author_email": "edmond.chuc@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4a/b7/1ba8b722ccc12b72b44af949f438a85111ba8db9e39f973dff4a47da068e/flask_htmx-0.4.0.tar.gz",
    "platform": null,
    "description": "##########\nFlask-HTMX\n##########\n\n.. image:: https://badge.fury.io/py/flask-htmx.svg\n    :target: https://badge.fury.io/py/flask-htmx\n\n.. image:: https://readthedocs.org/projects/flask-htmx/badge/?version=latest\n    :target: https://flask-htmx.readthedocs.io/en/latest/?badge=latest\n    :alt: Documentation Status\n\n\n.. image:: https://codecov.io/gh/edmondchuc/flask-htmx/branch/main/graph/badge.svg?token=K6YB3PB33T\n    :target: https://codecov.io/gh/edmondchuc/flask-htmx\n\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n    :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/License-MIT-red.svg\n    :target: https://github.com/edmondchuc/flask-htmx/blob/main/LICENSE\n\n.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week\n    :target: https://pepy.tech/project/flask-htmx\n\n.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month\n    :target: https://pepy.tech/project/flask-htmx\n\n.. image:: https://static.pepy.tech/personalized-badge/flask-htmx?period=total&units=international_system&left_color=grey&right_color=blue&left_text=downloads\n    :target: https://pepy.tech/project/flask-htmx\n\nA Flask extension to work with HTMX.\n\nDocumentation: https://flask-htmx.readthedocs.io\n\n.. quickstart-startblock\n\nQuickstart\n==========\n\nInstall the extension with pip.\n\n.. code-block:: bash\n\n    pip install flask-htmx\n\nOr perhaps you use Poetry.\n\n.. code-block:: bash\n\n    poetry add flask-htmx\n\nHTMX Request\n------------\n\nYou can register the HTMX object by passing the Flask\n:code:`app` object via the constructor.\n\n.. code-block:: python\n\n    htmx = HTMX(app)\n\nOr you can register the HTMX object using\n`HTMX.init_app() <https://flask-htmx.readthedocs.io/en/latest/flask_htmx.htmx.html#flask_htmx.htmx.HTMX.init_app>`_.\n\n.. code-block:: python\n\n    htmx = HTMX()\n    htmx.init_app(app)\n\nA minimal working example.\n\n.. code-block:: python\n\n    from flask import Flask, render_template\n    from flask_htmx import HTMX\n\n    app = Flask(__name__)\n    htmx = HTMX(app)\n\n    @app.route(\"/\")\n    def home():\n        if htmx:\n            return render_template(\"partials/thing.html\")\n        return render_template(\"index.html\")\n\nThe above example checks whether the request came\nfrom HTMX or not. If :code:`htmx` evaluates to\n`True <https://docs.python.org/3/library/constants.html#True>`_, then it was a HTMX request, else\n`False <https://docs.python.org/3/library/constants.html#False>`_.\n\nThis allows you to return a partial\nHTML when it's a HTMX request or the full page HTML\nwhen it is a normal browser request.\n\nFlask-HTMX also supports checking for HTMX headers\nduring a request in the view. For example, check\nthe current URL of the browser of a HTMX request.\n\n.. code-block:: python\n\n    @app.route(\"/\")\n    def home():\n        current_url = htmx.current_url\n        return render_template(\"index.html\", current_url=current_url)\n\nOther HTMX request headers are also available.\nSee https://htmx.org/reference/#request_headers.\n\nHTMX Response\n-------------\n\nYou might be interested on adding\n`htmx response headers <https://htmx.org/reference/#response_headers>`_ to your response.\nUse :code:`flask_htmx.make_response` for that. For example, instead of:\n\n.. code-block:: python\n\n    import json\n    from flask import make_response\n    from my_app import app\n\n    @app.route(\"/hola-mundo\")\n    def hola_mundo():\n        body = \"Hola Mundo!\"\n        response = make_response(body)\n        response.headers[\"HX-Push-URL\"] = \"false\"\n        trigger_string = json.dumps({\"event1\":\"A message\", \"event2\":\"Another message\"})\n        response.headers[\"HX-Trigger\"] = trigger_string\n        return response\n\nYou can do:\n\n.. code-block:: python\n\n    from flask_htmx import make_response\n    from my_app import app\n\n    @app.route(\"/hola-mundo\")\n    def hola_mundo():\n        body = \"Hola Mundo!\"\n        return make_response(\n            body,\n            push_url=False,\n            trigger={\"event1\": \"A message\", \"event2\": \"Another message\"},\n        )\n\n.. quickstart-endblock\n\nDocumentation\n=============\nVisit the `full documentation <https://flask-htmx.readthedocs.io>`_.\n\nDevelopment\n===========\n\nInstallation\n------------\n\n.. code-block:: bash\n\n    poetry install\n\nRunning tests\n-------------\n\n.. code-block:: bash\n\n    poetry run pytest\n\nCoverage\n--------\n\n.. code-block:: bash\n\n    poetry run pytest --cov=flask_htmx tests/\n\nDocs\n----\n\n.. code-block:: bash\n\n    sphinx-autobuild docs docs/_build/html\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Flask extension to work with HTMX.",
    "version": "0.4.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/edmondchuc/flask-htmx/issues",
        "Documentation": "https://flask-htmx.readthedocs.io",
        "Homepage": "https://github.com/edmondchuc/flask-htmx",
        "Repository": "https://github.com/edmondchuc/flask-htmx"
    },
    "split_keywords": [
        "flask",
        " htmx",
        " python",
        " web"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3d8e7e75c2210567ba11df9ea7d031eb5b8f45e82f6112cc8be885cb0ce86c7d",
                "md5": "b4c5c66260ee326e02461cc748b7f6a0",
                "sha256": "ac0ef976638bc635537a47c4ae622c91aef1e69d8bf52880aa9ae0db089ce7d2"
            },
            "downloads": -1,
            "filename": "flask_htmx-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4c5c66260ee326e02461cc748b7f6a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 6773,
            "upload_time": "2024-09-22T04:14:18",
            "upload_time_iso_8601": "2024-09-22T04:14:18.410859Z",
            "url": "https://files.pythonhosted.org/packages/3d/8e/7e75c2210567ba11df9ea7d031eb5b8f45e82f6112cc8be885cb0ce86c7d/flask_htmx-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ab71ba8b722ccc12b72b44af949f438a85111ba8db9e39f973dff4a47da068e",
                "md5": "9d3d98861f3e89c9f0d715929e7aa119",
                "sha256": "2d367fb27c8da99d031a0c566b7e562637139722e2d4e8ec67c7f941addb22fd"
            },
            "downloads": -1,
            "filename": "flask_htmx-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9d3d98861f3e89c9f0d715929e7aa119",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5815,
            "upload_time": "2024-09-22T04:14:20",
            "upload_time_iso_8601": "2024-09-22T04:14:20.006603Z",
            "url": "https://files.pythonhosted.org/packages/4a/b7/1ba8b722ccc12b72b44af949f438a85111ba8db9e39f973dff4a47da068e/flask_htmx-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-22 04:14:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "edmondchuc",
    "github_project": "flask-htmx",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "flask-htmx"
}
        
Elapsed time: 1.07702s