API¶
flask_inertia.inertia¶
Create a Flask extension to bind Flask and InertiaJS.
- class flask_inertia.inertia.Inertia(app: Flask | None = None)¶
Inertia Plugin for Flask.
- add_shorthand_route(url: str, component_name: str, endpoint: str | None = None) None¶
Connect a URL rule to a frontend component that does not need a controller.
This url does not have dedicated python source code but is linked to a JS component, (i.e. a frontend component which does not need props nor view_data).
- Parameters:
url – The URL rule as string as used in
flask.add_url_rulecomponent_name – Your frontend component name
endpoint – The endpoint for the registered URL rule. (by default
component_namein lower case)
- 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() Markup¶
Include JS router in Templates.
- init_app(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() Response | None¶
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: Response) 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
flask_inertia.views¶
Implement a method to add Inertia rendering into Flask.
- flask_inertia.views.always_include(prop_value: Any) AlwaysProp¶
Specify that a prop should always be included, even if it has not been explicitly required in a partial reload.
- Parameters:
prop_value – The props data or a callable wrapping the data
- flask_inertia.views.inertia_location(location: str) Response¶
Redirects to an external website, or even another non-Inertia endpoint.
Returns a server-side initiated window.location visit.
This method will generate a 409 Conflict response and include the destination URL in the X-Inertia-Location header. When this response is received client-side, Inertia will automatically perform a window.location = url visit.
from flask_inertia import inertia_location app = Flask(__name__) @app.route("/") def index(): return inertia_location("http"//www.foobar.com/")
- Parameters:
location – External location
- flask_inertia.views.lazy_include(callback: Callable) LazyProp¶
Specify that a prop should never be included unless explicitly requested using the
onlyoption.- Parameters:
callback – Callable wrapping the props data
- flask_inertia.views.render_inertia(component_name: str, props: Dict[str, Any] = {}, view_data: Dict[str, Any] = {}) 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 view_data={"description": "FooBar"}, # optional )
- Parameters:
component_name – The component name used in your frontend framework
props – A dict of properties used in your component
view_data – A dict of data that will not be sent to your JavaScript component
flask_inertia.props¶
Wrappers to implement lazy data evaluation for Inertia partial reloads.
- class flask_inertia.props.AlwaysProp(prop: Any)¶
Wrapper to specify that a prop should always be included, even if it has not been explicitly required.
- class flask_inertia.props.LazyProp(callback: Callable)¶
Wrapper to specify that a prop should never be included unless explicitly requested.
flask_inertia.version¶
Provide a method to calculate Flask assets version.
- flask_inertia.version.get_asset_version() str¶
Calculate asset version to allow Inertia to automatically make a full page visit in case of changes.
flask_inertia.unittest¶
- class flask_inertia.unittest.InertiaTestResponse(response: Iterable[bytes] | bytes | Iterable[str] | str | None = None, status: int | str | HTTPStatus | None = None, headers: Mapping[str, str | Iterable[str]] | Iterable[tuple[str, str]] | None = None, mimetype: str | None = None, content_type: str | None = None, direct_passthrough: bool = False)¶
Inertia test response wrapper.
You can use it in your unit test cases as followed:
from inertia.unittest import InertiaTestResponse class MyTestCase(unittest.TestCase): def setUp(self): self.app = create_app() # if you use an application factory self.app.response_class = InertiaTestResponse self.client = self.app.test_client() def test_lambda(self): response = self.client.get("/") data = response.inertia("root-id") self.assertEqual(data.component, "Dashboard")
- inertia(root_id: str) SimpleNamespace¶
Access inertia data stored in response.
Parse either the flask HTML response to extract the JSON encoded inertia page object or the JSON response based on its headers.
It will convert the page JSON object into a Python object using SimpleNamespace.
- Parameters:
root_id – Root component html id