The Inertia.js Flask Adapter allows you to seamlessly integrate Inertia.js with your Flask applications. This adapter provides the necessary tools to build modern, single-page applications using Flask as the backend and Inertia.js for the frontend.
- Install uv:
pip install uv- Create and activate virtual environment:
uv venv
source .venv/bin/activate # Unix/macOS- Install dependencies:
uv pip install -e .
uv pip install -r requirements.txt- For development:
uv pip install -r requirements-dev.txt- For testing:
uv pip install -r requirements-test.txt
## Configuration
You can initialize inertia-flask like most other extensions in Flask.
``` python
from flask import Flask
from inertia_flask import Inertia
# Required configuration keys
SECRET_KEY = "secret!"
INERTIA_TEMPLATE = "base.html" # Mandatory key
app = Flask(__name__)
app.config.from_object(__name__)
# Initialize Inertia
inertia = Inertia()
inertia.init_app(app)
# Alternatively, you can initialize it directly: inertia = Inertia(app)You can also initialize the Inertia extension on a specific Blueprint:
When using Inertia with Flask, you must choose between initializing Inertia on either:
- The main Flask application
- A Blueprint
You cannot initialize Inertia on both simultaneously. This is because Inertia manages page state and routing, which can lead to conflicts if multiple instances are running.
from flask import Blueprint, Flask
from flask_inertia import Inertia
# Required configuration keys
SECRET_KEY = "secret!"
INERTIA_TEMPLATE = "base.html" # Mandatory key
app = Flask(__name__)
app.config.from_object(__name__)
# Create a Blueprint
blueprint = Blueprint('inertia', __name__, template_folder='templates')
# Initialize Inertia on the Blueprint
inertia = Inertia(blueprint)
# Alternatively, you can initialize it directly: inertia = Inertia(blueprint)flask vite build: Builds Vite assets for productionflask vite dev: Runs Flask and Vite dev servers togetherflask vite install: Installs Vite dependencies
Flask does not provide CSRF protection by default. To handle CSRF protection, you can use the Flask Seasurf extension, which is a simple and effective solution for Flask applications.
Inertia.js uses Axios as the requests library. You can modify axios to integrate Seasurf with Inertia.js in your .js entry file as follows:
axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.xsrfCookieName = "_csrf_token";This ensures that Axios automatically includes the CSRF token in requests, aligning with Seasurf's protection mechanism.
The following configuration options can be set in your Flask application's config:
Use these settings for core Inertia functionality.
INERTIA_TEMPLATE(required): The base template used for rendering Inertia pagesINERTIA_JSON_ENCODER: Custom JSON encoder for serializing data (default:InertiaJsonEncoder)INERTIA_ENCRYPT_HISTORY: Enable encryption of Inertia history state (default:False)INERTIA_STATIC_ENDPOINT: Directory for static assets (default:"static", blueprints:"your_bp_name.static")
Use these settings to configure SSR support.
INERTIA_SSR_ENABLED: Enable server-side rendering support (default:False)INERTIA_SSR_URL: URL where the SSR server is running (default:"http://localhost:13714")
Use these settings to configure Vite.
-
INERTIA_VITE_DIR: Directory containing your Vite/frontend project (default:"inertia") -
INERTIA_VITE_ORIGIN: URL where Vite dev server runs (default:"http://localhost:5173") -
INERTIA_INTERNAL_VITE_ORIGIN: URL where Vite dev server runs relative to the flask app. This is useful when your exposed vite service and docker container have different locations or ports. (default:"http://localhost:5173") -
INERTIA_ROOT: Root element ID for mounting the Inertia app (default:"app")Use these settings to specify the manifest filenames.
INERTIA_VITE_MANIFEST_PATH(required): Client-side manifest file pathINERTIA_VITE_SSR_MANIFEST_PATH: Server-side manifest file path (default:None)
app.config.update(
INERTIA_TEMPLATE="base.html",
INERTIA_SSR_ENABLED=True,
INERTIA_VITE_DIR="frontend",
INERTIA_ROOT="app",
# Custom JSON encoder for special serialization needs
INERTIA_JSON_ENCODER=MyCustomJsonEncoder
)For blueprint-specific configuration, use prefixes:
app.config.update(
# Global settings
INERTIA_TEMPLATE="base.html",
# Blueprint-specific settings
BP_INERTIA_TEMPLATE="blueprint.html",
BP_INERTIA_VITE_DIR="bp_frontend"
)Ensure you have pnpm/npm installed and are on the latest version of node as Vite has dropped support for Node v21. If you are encountering issues around node and using Windows, try to sign out.
To run the example project, follow these steps:
uv venv
source .venv/bin/activate # Unix/macOS
uv pip install -e .
uv pip install -r requirements.txt
uv pip install -r requirements-dev.txt
cd examples/react
flask vite install
flask vite devTo contribute to the development of this extension, follow these steps:
-
Install the project dependencies with test support:
uv pip install -e . uv pip install -r requirements.txt uv pip install -r requirements-test.txt source .venv/bin/activate # Unix/macOS
-
Run the unit tests using pytest:
python -m pytest
- Install test dependencies:
uv pip install -r requirements-test.txt- Run tests using the test script:
./scripts/test.sh- Run specific test file:
./scripts/test.sh tests/test_inertia.py- Run tests with specific marker:
./scripts/test.sh -m "integration"- Run tests with output:
./scripts/test.sh -vThe test script automatically generates a coverage report. To generate an HTML coverage report:
./scripts/test.sh --cov-report=htmlThe report will be available in the htmlcov directory.
Parts of this repo were inspired by:
Inertia-django, MIT License, Copyright 2022 Bellawatt, Brandon Shar
Flask-inertia, MIT License, Copyright 2021, TROUVERIE Joachim jtrouverie@joakode.fr
Maintained and sponsored by IJACK Technologies.
