flask-htmx-fork


Nameflask-htmx-fork JSON
Version 0.2.2 PyPI version JSON
download
home_pagehttps://github.com/edmondchuc/flask-htmx
SummaryA Flask extension to work with HTMX. Forked.
upload_time2023-12-01 14:22:49
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
    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-fork",
    "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/40/1d/6ef9eb85df6b651a6c96d46aa55db09da63905bc1163f8b4dbec0cec0e98/flask_htmx_fork-0.2.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\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\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Flask extension to work with HTMX. Forked.",
    "version": "0.2.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": "1324d426c2853d89900e7693d2f1805521c50357af60fb2c9a7639500861bbca",
                "md5": "c6bed2b99a87f252bfc6c76220ccbd86",
                "sha256": "0ebbae6416038abf648885cab6489d5bb18f5b554b07cf7e2e9208651c988071"
            },
            "downloads": -1,
            "filename": "flask_htmx_fork-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c6bed2b99a87f252bfc6c76220ccbd86",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6823,
            "upload_time": "2023-12-01T14:22:47",
            "upload_time_iso_8601": "2023-12-01T14:22:47.695115Z",
            "url": "https://files.pythonhosted.org/packages/13/24/d426c2853d89900e7693d2f1805521c50357af60fb2c9a7639500861bbca/flask_htmx_fork-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "401d6ef9eb85df6b651a6c96d46aa55db09da63905bc1163f8b4dbec0cec0e98",
                "md5": "df83b837c3b35db64ebf7292bfecfd65",
                "sha256": "5b01fda643f354cb8792310422a50296ead864884c6ad772f57225ade57cc844"
            },
            "downloads": -1,
            "filename": "flask_htmx_fork-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "df83b837c3b35db64ebf7292bfecfd65",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5928,
            "upload_time": "2023-12-01T14:22:49",
            "upload_time_iso_8601": "2023-12-01T14:22:49.367173Z",
            "url": "https://files.pythonhosted.org/packages/40/1d/6ef9eb85df6b651a6c96d46aa55db09da63905bc1163f8b4dbec0cec0e98/flask_htmx_fork-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-01 14:22:49",
    "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-fork"
}
        
Elapsed time: 0.14687s