Skip to main content

Inertiajs Adapter for Flask.

Project description

coverage version inertiaversion license

Inertiajs Adapter for Flask.

Installation

$ pip install flask-inertia

Configuration

The module needs to be initialized the usual Flask way and can be configured using app.config keys:

from flask import Flask
from flask_inertia import Inertia

SECRET_KEY = "secret!"
# mandatory key
INERTIA_TEMPLATE = "base.html"

app = Flask(__name__)
app.config.from_object(__name__)

inertia = Inertia()
inertia.init_app(app)
# or inertia = Inertia(app)

The config key INERTIA_TEMPLATE must be used to set globally the template used by flask_inertia to render the server responses. This template must exists in the Flask app templates folder.

Use

For more information about InertiaJS, please read the docs provided by Inertia.

Whereas the Rails and Laravel adapters use a middleware to manage Inertia’s requests, this module doesn’t. Once it has been initiliazed it will create before_request and after_request hooks for your app to handle InertiaJS frontend requests.

Your templates

You will need to setup the root template that will be loaded on the first page visit. It will be used to load your site assets (CSS and JavaScript), and will also contain a root <div> to boot your JavaScript application in.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <title>My app</title>
    <link href="{{ url_for('static', filename='/css/app.css') }}" rel="stylesheet" />
    <script lang="javascript">
      {{ inertia.include_router() }}
    </script>
  </head>
  <body>
    <div id="app" data-page='{{ page | tojson }}'></div>
    <script src="{{ url_for('static', filename='/js/app.js') }}" defer></script>
  </body>
</html>

Your root div must set a HTML data-page attribute. It will be used by Flask, using a Page JSON object to communicate with Inertia.

To facilitate the route path resolving, the module provide a template context method called inertia.include_router. It will expose the Flask views resolution (like the url_for method) to your frontend Components.

This method has been extracted to django-js-routes package and works the same way via a window.reverseUrl JavaScript method ( https://github.com/ellmetha/django-js-routes#usage).

Create responses

This module provides a method render_inertia to render your frontend component through Flask responses. It will wrap your Responses and act accordingly to Inertia requests context responding a full html or a JSON reponse. It will be used instead of Flask render_template method:

from flask_inertia import render_inertia

@app.route("/test_inertia/")
def test_inertia():
    """An endpoint to test inertia integration."""
    data = {
        "username": "foo",
        "login": "bar",
    }
    return render_inertia(
        component_name="Index",
        props=data,
        template_name="base.html",
    )

This method take 2 arguments:

  • component_name: Your frontend component name (eg “Index” for an Index.vue Component for example)

  • props: [OPTIONAL] Data used by your component

Share data between requests

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.

This module provides a share method into the Inertia class to preassign shared data for each request. Shared data will be automatically merged with the page props provided in your controller. It takes as argument a key/value pair to serialize it in JSON in the responses.

You can set the shared data statically or programmatically using the method as followed:

inertia = Inertia(app)

# set statically a shared data
inertia.share("foo", "bar")

# or a computed value
def shared_value():
    return "buzz"

inertia.share("fizz", shared_value)

If the value is a callable, the module will resolve it during the response resolution.

To see a complete exemple on how to implement a project with this adapter, please read our Tutorial.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-inertia-0.2.tar.gz (20.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flask_inertia-0.2-py3.9.egg (16.9 kB view details)

Uploaded Egg

flask_inertia-0.2-py2.py3-none-any.whl (12.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flask-inertia-0.2.tar.gz.

File metadata

  • Download URL: flask-inertia-0.2.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.5

File hashes

Hashes for flask-inertia-0.2.tar.gz
Algorithm Hash digest
SHA256 60b55aa8068ecc425dec41c02d399bbdbb84355d09d393719c6837896030c308
MD5 bedcd7c72ed0a47860bc931c24d52913
BLAKE2b-256 e2fcb199792642c062542fcf042e376fac23e22a6010f2b19b8f30c8b880e49f

See more details on using hashes here.

File details

Details for the file flask_inertia-0.2-py3.9.egg.

File metadata

  • Download URL: flask_inertia-0.2-py3.9.egg
  • Upload date:
  • Size: 16.9 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.5

File hashes

Hashes for flask_inertia-0.2-py3.9.egg
Algorithm Hash digest
SHA256 eb7e9a4a92b2f11eb6f048dcba006289100504317cabf2b41f0b6693ae59a22a
MD5 e2f8466215eacce8e172dcc49590eb8f
BLAKE2b-256 f8c7730003190d083cbb63487f50824299f4a1a56aaffbeb9c288a93b30dd71a

See more details on using hashes here.

File details

Details for the file flask_inertia-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: flask_inertia-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.5

File hashes

Hashes for flask_inertia-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66a7dd2e819d8c8723e679b3eea1bfd6b7b5d57e53b6f6800145548567e6ace3
MD5 c59544a03d02ee3d4793f0001df6cfbc
BLAKE2b-256 08e4349becd83fc13579024ff7b147ae3d59ffdc172f18422fd7b98e557126b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page