API¶
flask_inertia.inertia¶
Create a Flask extension to bind Flask and InertiaJS.
-
class
flask_inertia.inertia.
Inertia
(app: Optional[flask.app.Flask] = None)¶ Inertia Plugin for Flask.
-
static
context_processor
()¶ Add an inertia directive to Jinja2 template to allow router inclusion
<head> <script lang="javascript"> {{ inertia.include_router() }} </script> </head>
-
include_router
() → markupsafe.Markup¶ Include JS router in Templates.
-
init_app
(app: flask.app.Flask)¶ Init as an app extension
Register before_request hook
Register after_request hook
Set context processor to have an inertia value in templates
-
process_incoming_inertia_requests
() → Optional[flask.wrappers.Response]¶ Process incoming Inertia requests.
AJAX requests must be forged by Inertia.
Whenever an Inertia request is made, Inertia will include the current asset version in the X-Inertia-Version header. If the asset versions are the same, the request simply continues as expected. However, if they are different, the server immediately returns a 409 Conflict response (only for GET request), and includes the URL in a X-Inertia-Location header.
Preassign shared data for each request.
Sometimes you need to access certain data on numerous pages within your application. For example, a common use-case for this is showing the current user in the site header. Passing this data manually in each response isn’t practical. In these situations shared data can be useful.
- Parameters
key – Data key to share between requests
value – Data value or Function returning the data value
-
update_redirect
(response: flask.wrappers.Response) → flask.wrappers.Response¶ Update redirect to set 303 status code.
409 conflict responses are only sent for GET requests, and not for POST/PUT/PATCH/DELETE requests. That said, they will be sent in the event that a GET redirect occurs after one of these requests. To force Inertia to use a GET request after a redirect, the 303 HTTP status is used
- Parameters
response – The generated response to update
-
static
flask_inertia.views¶
Implement a method to add Inertia rendering into Flask.
-
flask_inertia.views.
render_inertia
(component_name: str, props: dict = {}) → flask.wrappers.Response¶ Method to use instead of Flask render_template.
Returns either a JSON response or a HTML response including a JSON encoded inertia page object according to Inertia request headers.
from flask_inertia import render_inertia app = Flask(__name__) @app.route("/") def index(): data = { "username": "foo", "login": "bar", } return render_inertia( component_name="Index", # this must exists in your frontend props=data, # optional )
- Parameters
component_name – The component name used in your frontend framework
props – A dict of properties used in your component