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.

share(key: str, value: Any)

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

flask_inertia.views

Implement a method to add Inertia rendering into Flask.

flask_inertia.views.render_inertia(component_name: str, props: Dict[str, Any] = {}) 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

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: Optional[Union[Iterable[bytes], bytes, Iterable[str], str]] = None, status: Optional[Union[int, str, http.HTTPStatus]] = None, headers: Optional[Union[Mapping[str, Union[str, int, Iterable[Union[str, int]]]], Iterable[Tuple[str, Union[str, int]]]]] = None, mimetype: Optional[str] = None, content_type: Optional[str] = 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) types.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