flask-htmx


Nameflask-htmx JSON
Version 0.3.2 PyPI version JSON
download
home_pagehttps://github.com/edmondchuc/flask-htmx
SummaryA Flask extension to work with HTMX.
upload_time2023-12-16 06:41:13
maintainer
docs_urlNone
authorEdmond Chuc
requires_python>=3.8,<4.0
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": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "Flask,HTMX,Python,Web",
    "author": "Edmond Chuc",
    "author_email": "edmond.chuc@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/36/cd/82775dcb3b706e5fb4b3417bdb5dddd430de9df2acbd25d6ce3bb00be1df/flask_htmx-0.3.2.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.3.2",
    "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": "81111a978d19a2bf3b8667541df2535da8532059ec44b5a27805b8867f0b49a2",
                "md5": "9a18aa61e480e4bd72b3f230aeae3061",
                "sha256": "a1e0071216349197d6669662c2c35a1ab849b6fca28c89dc90932761f7e73c05"
            },
            "downloads": -1,
            "filename": "flask_htmx-0.3.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9a18aa61e480e4bd72b3f230aeae3061",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6736,
            "upload_time": "2023-12-16T06:41:11",
            "upload_time_iso_8601": "2023-12-16T06:41:11.801158Z",
            "url": "https://files.pythonhosted.org/packages/81/11/1a978d19a2bf3b8667541df2535da8532059ec44b5a27805b8867f0b49a2/flask_htmx-0.3.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "36cd82775dcb3b706e5fb4b3417bdb5dddd430de9df2acbd25d6ce3bb00be1df",
                "md5": "82b196aa81842535b24bcb3c75e1b0a0",
                "sha256": "8def77bb292369ff77513ff7b76d27b06f83e1d8c21165b6714c06c1cc2b9275"
            },
            "downloads": -1,
            "filename": "flask_htmx-0.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "82b196aa81842535b24bcb3c75e1b0a0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5786,
            "upload_time": "2023-12-16T06:41:13",
            "upload_time_iso_8601": "2023-12-16T06:41:13.476699Z",
            "url": "https://files.pythonhosted.org/packages/36/cd/82775dcb3b706e5fb4b3417bdb5dddd430de9df2acbd25d6ce3bb00be1df/flask_htmx-0.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-16 06:41:13",
    "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: 0.14897s