{"id":13780518,"url":"https://github.com/sponsfreixes/htmx-flask","last_synced_at":"2025-04-11T11:31:55.515Z","repository":{"id":63017415,"uuid":"545282135","full_name":"sponsfreixes/htmx-flask","owner":"sponsfreixes","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-29T17:30:25.000Z","size":134,"stargazers_count":31,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T13:39:14.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sponsfreixes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-04T04:58:22.000Z","updated_at":"2025-02-26T06:03:06.000Z","dependencies_parsed_at":"2022-11-11T01:31:02.917Z","dependency_job_id":null,"html_url":"https://github.com/sponsfreixes/htmx-flask","commit_stats":{"total_commits":15,"total_committers":1,"mean_commits":15.0,"dds":0.0,"last_synced_commit":"f0f1779ad80ef0eab0dbaaf315cf1fd1d17ca05d"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sponsfreixes%2Fhtmx-flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sponsfreixes%2Fhtmx-flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sponsfreixes%2Fhtmx-flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sponsfreixes%2Fhtmx-flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sponsfreixes","download_url":"https://codeload.github.com/sponsfreixes/htmx-flask/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248146108,"owners_count":21055271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:16.667Z","updated_at":"2025-04-11T11:31:55.022Z","avatar_url":"https://github.com/sponsfreixes.png","language":"Python","funding_links":[],"categories":["Third Party Packages 📦 \u003ca name = \"tools\"\u003e\u003c/a\u003e"],"sub_categories":["Helper Libraries"],"readme":"# htmx-Flask\n\nhtmx-Flask is an extension for Flask that adds support for [htmx](https://htmx.org) to \nyour application.  It simplifies using htmx with Flask by enhancing the global `request` \nobject and providing a new `make_response` function.\n\n## Install\n\nIt's just `pip install htmx-flask` and you're all set. It's a pure Python package that\nonly needs [`flask`](https://flask.palletsprojects.com) (for obvious reasons!).\n\n## Usage\n\n### Htmx Request\n\nBefore using the enhanced `request`, you need to initialize the extension with:\n\n```python\nfrom flask import Flask\nfrom htmx_flask import Htmx\n\nhtmx = Htmx()\n\napp = Flask(__name__)\nhtmx.init_app(app)\n```\n\nAfter that, you can use `htmx_flask.request.htmx` to easily access\n[htmx request headers](https://htmx.org/reference/#request_headers). For example,\ninstead of:\n\n```python\nfrom flask import request\nfrom my_app import app\n\n@app.route(\"/\")\ndef hello_workd():\n    if request.headers.get(\"HX-Request\") == \"true\":\n        is_boosted = \"Yes!\" if request.headers.get(\"HX-Boosted\") == \"true\" else \"No!\"\n        current_url = request.headers.get(\"HX-Current-URL\")\n        return (\n            \"\u003cp\u003eHello World triggered from a htmx request.\u003c/p\u003e\"\n            f\"\u003cp\u003eBoosted: {is_boosted}\u003c/p\u003e\"\n            f\"\u003cp\u003eThe current url is {current_url}.\"\n        )\n    else:\n        return \"\u003cp\u003eHello World triggered from a regular request.\u003c/p\u003e\"\n```\n\nYou can do:\n\n```python\nfrom htmx_flask import request\nfrom my_app import app\n\n@app.route(\"/\")\ndef hello_workd():\n    if request.htmx:\n        is_boosted = \"Yes!\" if request.htmx.boosted else \"No!\"\n        current_url = request.htmx.current_url\n        return (\n            \"\u003cp\u003eHello World triggered from a htmx request.\u003c/p\u003e\"\n            f\"\u003cp\u003eBoosted: {is_boosted}\u003c/p\u003e\"\n            f\"\u003cp\u003eThe current url is {current_url}.\"\n        )\n    else:\n        return \"\u003cp\u003eHello World triggered from a regular request.\u003c/p\u003e\"\n```\n\n### Htmx response\n\nYou might be interested on adding\n[htmx response headers](https://htmx.org/reference/#response_headers) to your response.\nUse `htmx_flask.make_response` for that. For example, instead of:\n\n```python\nimport json\nfrom flask import make_response\nfrom my_app import app\n\n@app.route(\"/hola-mundo\")\ndef 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```\n\nYou can do:\n\n```python\nfrom htmx_flask import make_response\nfrom my_app import app\n\n@app.route(\"/hola-mundo\")\ndef 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\n# IntelliSense\n\nBy using htmx-Flask you will also get the benefits of code completion, parameter info\nand quick info on your IDE. Check out these screenshots from PyCharm:\n\n![request.htmx autocomplete](https://raw.githubusercontent.com/sponsfreixes/htmx-flask/main/docs/images/request_htmx_code_completion.png)\n\n![make_response quick info](https://raw.githubusercontent.com/sponsfreixes/htmx-flask/main/docs/images/make_response_quick_info.png)\n\n![make_response parameter info](https://raw.githubusercontent.com/sponsfreixes/htmx-flask/main/docs/images/make_response_parameter_info.png)\n\n## How to contribute\n\nThis project uses pre-commit hooks to run black, isort, pyupgrade and flake8 on each commit. To have that running\nautomatically on your environment, install the project with:\n\n```shell\npip install -e .[dev]\n```\n\nAnd then run once:\n\n```shell\npre-commit install\n```\n\nFrom now one, every time you commit your files on this project, they will be automatically processed by the tools listed\nabove.\n\n## How to run tests\n\nYou can install pytest and other required dependencies with:\n\n```shell\npip install -e .[tests]\n```\n\nAnd then run the test suite with:\n\n```shell\npytest\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsponsfreixes%2Fhtmx-flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsponsfreixes%2Fhtmx-flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsponsfreixes%2Fhtmx-flask/lists"}