{"id":13805423,"url":"https://github.com/j0ack/flask-inertia","last_synced_at":"2025-03-15T16:08:11.793Z","repository":{"id":57430408,"uuid":"362493111","full_name":"j0ack/flask-inertia","owner":"j0ack","description":"Mirror of https://git.joakode.fr/joack/flask-inertia/","archived":false,"fork":false,"pushed_at":"2024-09-08T13:05:02.000Z","size":288,"stargazers_count":45,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-28T10:51:43.601Z","etag":null,"topics":["flask","inertiajs"],"latest_commit_sha":null,"homepage":"https://flask-inertia.readthedocs.io","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/j0ack.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG","contributing":"CONTRIBUTING.rst","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":"2021-04-28T14:16:51.000Z","updated_at":"2025-02-28T10:23:40.000Z","dependencies_parsed_at":"2023-02-09T02:31:26.023Z","dependency_job_id":"383e346d-23d6-4f6a-b4b7-6b3375a8bfc4","html_url":"https://github.com/j0ack/flask-inertia","commit_stats":{"total_commits":28,"total_committers":3,"mean_commits":9.333333333333334,"dds":0.3928571428571429,"last_synced_commit":"f05e284d392001dcbf8e27ca9dc79ec4c1771ed4"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j0ack%2Fflask-inertia","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j0ack%2Fflask-inertia/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j0ack%2Fflask-inertia/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j0ack%2Fflask-inertia/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/j0ack","download_url":"https://codeload.github.com/j0ack/flask-inertia/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243754094,"owners_count":20342542,"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":["flask","inertiajs"],"created_at":"2024-08-04T01:01:00.982Z","updated_at":"2025-03-15T16:08:11.762Z","avatar_url":"https://github.com/j0ack.png","language":"Python","funding_links":[],"categories":["Adapters"],"sub_categories":["Server-side"],"readme":"Flask-Inertia\n=============\n\n|coverage| |version| |inertiaversion| |license|\n\n\n`Inertiajs \u003chttps://inertiajs.com/\u003e`_ Adapter for `Flask \u003chttps://flask.palletsprojects.com/\u003e`_.\n\n\nInstallation\n------------\n\n.. code-block:: bash\n\n   $ pip install flask-inertia\n\nConfiguration\n-------------\n\nThe module needs to be initialized the usual Flask way and can be configured using\n``app.config`` keys::\n\n  from flask import Flask\n  from flask_inertia import Inertia\n\n  SECRET_KEY = \"secret!\"\n  # mandatory key\n  INERTIA_TEMPLATE = \"base.html\"\n\n  app = Flask(__name__)\n  app.config.from_object(__name__)\n\n  inertia = Inertia()\n  inertia.init_app(app)\n  # or inertia = Inertia(app)\n\n\nThe config key ``INERTIA_TEMPLATE`` must be used to set globally the template used by\n``flask_inertia`` to render the server responses. This template must exists in the\nFlask app ``templates`` folder.\n\nUse\n---\n\nFor more information about InertiaJS, please\n`read the docs provided by Inertia \u003chttps://inertiajs.com/\u003e`_.\n\nWhereas the Rails and Laravel adapters use a middleware to manage Inertia's\nrequests, this module doesn't. Once it has been initiliazed it will create\n``before_request`` and ``after_request`` hooks for your app to handle InertiaJS\nfrontend requests.\n\nYour templates\n++++++++++++++\n\nYou will need to setup the root template that will be loaded on the first page\nvisit. It will be used to load your site assets (CSS and JavaScript), and will\nalso contain a root ``\u003cdiv\u003e`` to boot your JavaScript application in.\n\n.. code:: jinja\n\n  \u003c!DOCTYPE html\u003e\n  \u003chtml\u003e\n    \u003chead\u003e\n      \u003cmeta charset=\"utf-8\" /\u003e\n      \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\" /\u003e\n      \u003ctitle\u003eMy app\u003c/title\u003e\n      \u003clink href=\"{{ url_for('static', filename='/css/app.css') }}\" rel=\"stylesheet\" /\u003e\n      \u003cscript lang=\"javascript\"\u003e\n        {{ inertia.include_router() }}\n      \u003c/script\u003e\n    \u003c/head\u003e\n    \u003cbody\u003e\n      \u003cdiv id=\"app\" data-page='{{ page | tojson }}'\u003e\u003c/div\u003e\n      \u003cscript src=\"{{ url_for('static', filename='/js/app.js') }}\" defer\u003e\u003c/script\u003e\n    \u003c/body\u003e\n  \u003c/html\u003e\n\n\n.. warning:: Be aware of the single quotes used for the ``data-page`` argument.\n   Jinja2 will not escape the double quotes of your JSON object\n   https://github.com/pallets/flask/issues/1002\n\nYour root ``div`` must set a HTML ``data-page`` attribute. It will be used by Flask,\nusing a `Page JSON object \u003chttps://inertiajs.com/the-protocol#the-page-object\u003e`_\nto communicate with Inertia.\n\nTo facilitate the route path resolving, the module provide a template context method\ncalled ``inertia.include_router``. It will expose the Flask views resolution (like\nthe ``url_for`` method) to your frontend Components.\n\nThis method has been extracted to `django-js-routes \u003chttps://github.com/ellmetha/django-js-routes\u003e`_\npackage and works the same way via a ``window.reverseUrl`` JavaScript method (\nhttps://github.com/ellmetha/django-js-routes#usage).\n\nCreate responses\n++++++++++++++++\n\nThis module provides a method ``render_inertia`` to render your frontend component\nthrough Flask responses. It will wrap your Responses and act accordingly to Inertia\nrequests context responding a full html or a JSON reponse. It will be used instead\nof Flask ``render_template`` method::\n\n  from flask.typing import ResponseReturnValue\n  from flask_inertia import render_inertia\n\n  @app.route(\"/test_inertia/\")\n  def test_inertia() -\u003e ResponseReturnValue:\n      \"\"\"An endpoint to test inertia integration.\"\"\"\n      data = {\n          \"username\": \"foo\",\n          \"login\": \"bar\",\n      }\n      return render_inertia(\n          component_name=\"Index\",\n          props=data,\n          view_data={},\n      )\n\nThis method take 3 arguments:\n\n  * ``component_name``: Your frontend component name (e.g. \"Index\" for an Index.vue\n    Component for example)\n  * ``props``: [OPTIONAL] Data used by your component\n  * ``view_data``: [OPTIONAL] Data used in your template but not sent to your JavaScript\n    components\n\nShorthand routes\n++++++++++++++++\n\nIf you have a page that does not need a corresponding controller method (i.e. a frontend\ncomponent which does not need ``props`` nor ``view_data``), like a \"FAQ\" or \"about\" page,\nyou can route directly to a component via the ``add_shorthand_route`` method::\n\n  from flask import Flask\n  from flask_inertia import Inertia\n\n  app = Flask(__name__)\n  app.config.from_object(__name__)\n  inertia = Inertia(app)\n\n  inertia.add_shorthand_route(\"/faq/\", \"FAQ\")\n  inertia.add_shorthand_route(\"/about/\", \"About\", \"My About Page\")\n\n\nThis method takes 3 arguments:\n\n  * ``url``: The URL rule as string as used in ``flask.add_url_rule``\n  * ``component_name``: Your frontend component name (e.g. \"Index\" for an Index.vue\n    Component for example)\n  * ``endpoint`` [OPTIONAL]: The endpoint for the registered URL rule. (by default the\n    ``component_name`` in lower case)\n\nRoot template data\n++++++++++++++++++\n\nThere are situations where you may want to access your prop data in your root Jinja2\ntemplate. These props are available via the ``page`` variable.\n\n.. code:: jinja\n\n   \u003cmeta name=\"author\" content=\"{{ page['props']['username'] }}\"\u003e\n\nYou may want to provide data that will not be sent to your JavaScript components.\nYou can do this using the ``view_data`` dictionnary in the ``render_inertia`` method::\n\n  return render_inertia(\n      component_name=\"Index\",\n      props=data,\n      view_data={\n          \"description\": \"A test page\"\n      }\n  )\n\nYou can then access this variable with the template variable ``view_data``.\n\n.. code:: jinja\n\n   \u003cmeta name=\"content\" content=\"{{ view_data['description'] }}\"\u003e\n\nExternal redirects\n++++++++++++++++++\n\nIt is possible to redirect to an external website, or even another non-Inertia endpoint\nin your app while handling an Inertia request. This can be accomplished using a\nserver-side initiated ``window.location`` visit via the ``inertia_location`` method::\n\n  from flask.typing import ResponseReturnValue\n  from flask_inertia import inertia_location\n\n  @app.route(\"/test_inertia/\")\n  def external_url() -\u003e ResponseReturnValue:\n      return inertia_location(\"http://foobar.com/\")\n\n\nIt will generate a ``409 Conflict`` response and include the destination URL in\nthe ``X-Inertia-Location`` header. When this response is received client-side,\nInertia will automatically perform a ``window.location = url`` visit.\n\nShare data between requests\n+++++++++++++++++++++++++++\n\nSometimes you need to access certain data on numerous pages within your application.\nFor example, a common use-case for this is showing the current user in the site\nheader. Passing this data manually in each response isn't practical. In these\nsituations shared data can be useful.\n\nThis module provides a ``share`` method into the ``Inertia`` class to preassign\nshared data for each request. Shared data will be automatically merged with the\npage ``props`` provided in your controller. It takes as argument a key/value pair\nto serialize it in JSON in the responses.\n\nYou can set the shared data statically or programmatically using the method as\nfollowed::\n\n  inertia = Inertia(app)\n\n  # set statically a shared data\n  inertia.share(\"foo\", \"bar\")\n\n  # or a computed value\n  def shared_value() -\u003e str:\n      return \"buzz\"\n\n  inertia.share(\"fizz\", shared_value)\n\nIf the value is a ``callable``, the module will resolve it during the response\nresolution.\n\nLazy data evaluation\n++++++++++++++++++++\n\nWhen making visits to the same page you are already on, it's not always necessary\nto re-fetch all of the page's data from the server. In fact, selecting only a subset\nof the data can be a helpful performance optimization if it's acceptable that\nsome page data becomes stale.\n\nFor partial reloads to be most effective, be sure to also use lazy data evaluation\nwhen returning props from your server-side routes or controllers. This can be\naccomplished by wrapping all optional page data in a ``callable``::\n\n  from flask.typing import ResponseReturnValue\n  from flask_inertia import render_inertia\n\n  def get_users() -\u003e list[User]:\n      return User.query.all()\n\n  @app.route(\"/users/\")\n  def users_view() -\u003e ResponseReturnValue:\n      return render_inertia(\n          \"Users\",\n          props={\n              \"users\": get_users,\n              \"companies\": Company.query.all(),\n          }\n      )\n\nWhen Inertia performs a request, it will determine which data is required and only\nthen will it evaluate the callable. This can significantly increase the performance\nof pages that contain a lot of optional data.\n\nAdditionally, this module provides an ``lazy_include`` method to specify that a prop\nshould never be included unless explicitly requested using the only ``option``. And\non the inverse, you can use the ``always_include`` method to specify that a prop\nshould always be included, even if it has not been explicitly required in a partial\nreload::\n\n  from flask.typing import ResponseReturnValue\n  from flask_inertia import always_include, lazy_include, render_inertia\n\n  def get_users() -\u003e list[User]:\n      return User.query.all()\n\n\n  @app.route(\"/users/\")\n  def users_view() -\u003e ResponseReturnValue:\n      return render_inertia(\n          \"Users\",\n          props={\n              # ALWAYS included on standard visits\n              # OPTIONALLY included on partial reloads\n              # ALWAYS evaluated\n              \"users\": User.query.all(),  # or get_users()\n\n              # ALWAYS included on standard visits\n              # OPTIONALLY included on partial reloads\n              # ONLY evaluated when needed\n              \"users\": get_users,\n\n              # NEVER included on standard visits\n              # OPTIONALLY included on partial reloads\n              # ONLY evaluated when needed\n              \"users\": lazy_include(get_users),\n\n              # ALWAYS included on standard visits\n              # ALWAYS included on partial reloads\n              # ALWAYS evaluated\n              \"users\": always_include(User.query.all()),  # or always_include(get_users())\n          }\n      )\n\nTo see a complete exemple on how to implement a project with this adapter, please\nread our `Tutorial \u003chttps://flask-inertia.readthedocs.io/en/latest/tutorial.html\u003e`_.\n\nContributing\n------------\n\nIf you want to contribute to this project, please read the dedicated file :\n`CONTRIBUTING.rst`.\n\n\n.. |coverage| image:: https://git.joakode.fr/joack/flask-inertia/badges/main/coverage.svg\n.. |version| image:: https://img.shields.io/pypi/v/flask-inertia.svg\n.. |license| image:: https://img.shields.io/github/license/j0ack/flask-inertia.svg\n.. |inertiaversion| image:: https://img.shields.io/badge/inertia-1.2-cyan\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj0ack%2Fflask-inertia","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fj0ack%2Fflask-inertia","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj0ack%2Fflask-inertia/lists"}