# Inertia.js FastAPI Adapter
<!-- TOC -->
- [Inertia.js FastAPI Adapter](#inertiajs-fastapi-adapter)
- [Installation](#installation)
- [Configuration](#configuration)
- [Examples](#examples)
- [Usage](#usage)
- [Create a Jinja2Template](#create-a-jinja2template)
- [Set up the dependency](#set-up-the-dependency)
- [Rendering a page](#rendering-a-page)
- [Rendering assets](#rendering-assets)
- [Sharing data](#sharing-data)
- [Flash messages](#flash-messages)
- [Flash errors](#flash-errors)
- [Redirect to an external URL](#redirect-to-an-external-url)
- [Redirect back](#redirect-back)
- [Enable SSR](#enable-ssr)
- [Frontend documentation](#frontend-documentation)
- [For a classic build](#for-a-classic-build)
- [For a SSR build](#for-a-ssr-build)
- [Performance note](#performance-note)
## Installation
You can install the package via pip:
```bash
pip install fastapi-inertia
```
## Configuration
You can configure the adapter by passing a `InertiaConfig` object to the `Inertia` class.
The following options are available:
| key | default | options | description |
| ---------------------- | ---------------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| environment | development | development,production | The environment to use |
| version | 1.0.0 | Any valid string | The version of your server |
| json_encoder | InertiaJsonEncoder | Any class that extends json.JSONEncoder | The JSON encoder used to encode page data when HTML is returned |
| manifest_json_path | "" | Any valid path | The path to the manifest.json file. Needed in production |
| dev_url | http://localhost:5173 | Any valid url | The URL to the development server |
| ssr_url | http://localhost:13714 | Any valid url | The URL to the SSR server |
| ssr_enabled | False | True,False | Whether to [enable SSR](#enable-ssr). You need to install the `httpx` package, to have set the manifest_json_path and started the SSR server |
| root_directory | src | Any valid path | The directory in which is located the javascript code in your frontend. Will be used to find the relevant files in your manifest.json. |
| entrypoint_filename | main.js | Any valid file | The entrypoint for you frontend. Will be used to find the relevant files in your manifest.json. |
| assets_prefix | "" | Any valid string | An optional prefix for your assets. Will prefix the links generated from the assets mentioned in manifest.json. |
| use_flash_messages | False | True,False | Whether to use [flash messages](#flash-messages). You need to use Starlette's SessionMiddleware to use this feature |
| flash_message_key | messages | Any valid string | The key to use for [flash errors](#flash-errors) |
| use_flash_errors | False | True,False | Whether to use flash errors |
| flash_error_key | errors | Any valid string | The key to use for flash errors |
| templates | None | A Jinja2Templates instance | The templates instance in which Inertia will look for the `root_template_filename` template |
| root_template_filename | index.html | Any valid jinja2 template file | The file which will be used to render your inertia application |
## Examples
You can see different full examples in the `examples` directory
## Usage
### Create a Jinja2Template
In order to use the Inertia.js adapter, you have to create a Jinja2Template that the library will use.
It **must** have both an `inertia_head` and an `inertia_body` tag in it.
- `inertia_head` is where the library will place the code that supposedly goes inside the HTML `head` tag
- `inertia_body` is where the library will place the code that supposedly goes inside the HTML `body` tag
You can find the simplest example in `inertia/tests/templates/index.html`.
You should then register the folder in which you put this file as the directory of your Jinja2Templates
```python
templates = Jinja2Templates(directory=template_dir)
```
This option should be passed to the InertiaConfig class presented below, under the `templates` key.
If you choose a different template file name than `index.html`, you can also pass the `root_template_filename` key with, as value, your template file name.
### Set up the dependency
This Inertia.js adapter has been developed to be used as a FastAPI dependency.
To use it, you first need to set up the dependency, with your desired configuration.
`inertia_dependency.py`
```python
from fastapi import Depends
from typing import Annotated
from inertia import InertiaConfig, inertia_dependency_factory, Inertia
inertia_config = InertiaConfig(
# Your desired configuration
)
inertia_dependency = inertia_dependency_factory(
inertia_config
)
InertiaDependency = Annotated[Inertia, Depends(inertia_dependency)]
```
You can then access the `InertiaDependency` in your route functions, and use it to render your pages.
### Rendering a page
To render a page, you can use the `render` method of the `Inertia` class. It takes two arguments:
- The name of the page
- The data to pass to the page
`main.py`
```python
from fastapi import FastAPI, Depends
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
@app.get('/', response_model=None)
async def index(inertia: InertiaDependency) -> InertiaResponse:
return inertia.render('Index', {
'name': 'John Doe'
})
```
### Rendering assets
As your front-end framework likely references assets that are not served by FastAPI,
you need to mount a static directory to serve these assets.
`main.py`
```python
import os
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from inertia_dependency import inertia_config
app = FastAPI()
webapp_dir = (
os.path.join(os.path.dirname(__file__), "..", "webapp", "dist")
if inertia_config.environment != "development"
else os.path.join(os.path.dirname(__file__), "..", "webapp", "src")
)
app.mount("/src", StaticFiles(directory=webapp_dir), name="src")
app.mount(
"/assets", StaticFiles(directory=os.path.join(webapp_dir, "assets")), name="assets"
)
```
### Sharing data
To share data, in Inertia, is basically to add data before even entering your route.
This is useful, for example, to add a user to all your pages that expects your user to be logged in.
`main.py`
```python
from fastapi import FastAPI, Depends
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
def current_user(inertia: InertiaDependency):
inertia.share(user={
'name': 'John Doe'
})
@app.get('/', response_model=None, dependencies=[Depends(current_user)])
async def index(inertia: InertiaDependency) -> InertiaResponse:
"""
Because of the dependency, and as we are sharing the user data, the user data will be available in the page.
"""
return inertia.render('Index')
```
### Flash messages
With the inertia dependency, you have access to a `flash` helper method that allows you to add flash messages to your pages.
This is useful to display messages to the user after a form submission, for example.
Those messages are called `flash` messages as they are only displayed once.
You need to have set `use_flash_messages` to `True` in your configuration to use this feature.
You need to have the `SessionMiddleware` enabled in your application to use this feature.
`main.py`
```python
from fastapi import FastAPI, Depends
from starlette.middleware.sessions import SessionMiddleware
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
app.add_middleware(SessionMiddleware, secret_key="secret")
@app.get('/', response_model=None)
async def index(inertia: InertiaDependency) -> InertiaResponse:
inertia.flash('Index was reached successfully', category='success')
return inertia.render('Index')
```
### Flash errors
If you handle form submissions in your application, and if you do all validation at the pydantic level,
a malformed payload will raise a `RequestValidationError` exception.
You can use the `inertia_request_validation_exception_handler` to handle this exception and display the errors to the user.
It supports error bags, so you can display multiple errors at once.
If the request is not from Inertia, it will fallback to FastAPI's default error handling.
In order to use this feature, you need to have set `use_flash_errors` to `True` in your configuration.
You also need to have the `SessionMiddleware` enabled in your application to use this feature.
`main.py`
```python
from fastapi import FastAPI, Depends
from pydantic import BaseModel, model_validator
from typing import Any
from fastapi.exceptions import RequestValidationError
from starlette.middleware.sessions import SessionMiddleware
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler, inertia_request_validation_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
app.add_exception_handler(RequestValidationError, inertia_request_validation_exception_handler)
app.add_middleware(SessionMiddleware, secret_key="secret")
class Form(BaseModel):
name: str
@model_validator(mode="before")
@classmethod
def name_must_contain_doe(cls, data: Any):
if 'Doe' not in data.name:
raise ValueError('Name must contain Doe')
@app.post('/', response_model=None)
async def index(data: Form, inertia: InertiaDependency) -> InertiaResponse:
return inertia.render('Index')
```
### Redirect to an external URL
If you want to redirect the user to an external URL, you can use the `location` method of the `Inertia` class.
It takes one argument: the URL to redirect to.
`main.py`
```python
from fastapi import FastAPI, Depends
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
@app.get('/', response_model=None)
async def index(inertia: InertiaDependency) -> InertiaResponse:
return inertia.location('https://google.fr')
```
### Redirect back
If you want to redirect the user back (for example, after a form submission), you can use the `back` method of the `Inertia` class.
It will use the `Referer` header to redirect the user back.
If you're on a `GET` request, the status code will be `307`. Otherwise, it will be `303`.
That ways, it will trigger a new GET request to the referer URL.
`main.py`
```python
from fastapi import FastAPI, Depends
from inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler
from inertia_dependency import InertiaDependency
app = FastAPI()
app.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)
@app.get('/', response_model=None)
async def index(inertia: InertiaDependency) -> InertiaResponse:
return inertia.back()
```
### Enable SSR
To enable SSR, you need to set `ssr_enabled` to `True` in your configuration.
You also need to have set the `manifest_json_path` to the path of your `manifest.json` file.
You need to have the `httpx` package installed to use this feature.
This can be done through the following command:
```bash
pip install httpx
```
## Frontend documentation
There is no particular caveats to keep in mind when using this adapter.
However, here's an example of how you would set up your frontend to work with this adapter.
### For a classic build
> [!NOTE]
> To build the project, you can run the `vite build` command
`vite.config.js`
```javascript
import { fileURLToPath } from "node:url";
import { dirname } from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
const projectRoot = dirname(fileURLToPath(import.meta.url));
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
"@": `${projectRoot}/src`,
},
},
build: {
manifest: "manifest.json",
outDir: "dist",
rollupOptions: {
input: "src/main.js",
},
},
});
```
`main.js`
```javascript
import { createApp, h } from "vue";
import { createInertiaApp } from "@inertiajs/vue3";
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob("./Pages/**/*.vue", { eager: true });
return pages[`./Pages/${name}.vue`];
},
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el);
},
});
```
### For a SSR build
> [!NOTE]
> To build the project, you can run the `vite build` and `vite build --ssr` commands
> To serve the Inertia SSR server, you can run the `node dist/ssr/ssr.js` command
`vite.config.js`
```javascript
import { fileURLToPath } from "node:url";
import { dirname } from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
const projectRoot = dirname(fileURLToPath(import.meta.url));
// https://vitejs.dev/config/
export default defineConfig(({ isSsrBuild }) => ({
plugins: [vue()],
resolve: {
alias: {
"@": `${projectRoot}/src`,
},
},
build: {
manifest: isSsrBuild ? false : "manifest.json",
outDir: isSsrBuild ? "dist/ssr" : "dist/client",
rollupOptions: {
input: isSsrBuild ? "src/ssr.js" : "src/main.js",
},
},
}));
```
`main.js`
```javascript
import { createSSRApp, h } from "vue";
import { createInertiaApp } from "@inertiajs/vue3";
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob("./Pages/**/*.vue", { eager: true });
return pages[`./Pages/${name}.vue`];
},
setup({ el, App, props, plugin }) {
createSSRApp({ render: () => h(App, props) })
.use(plugin)
.mount(el);
},
});
```
`ssr.js`
```javascript
import { createInertiaApp } from "@inertiajs/vue3";
import createServer from "@inertiajs/vue3/server";
import { renderToString } from "@vue/server-renderer";
import { createSSRApp, h } from "vue";
createServer((page) =>
createInertiaApp({
page,
render: renderToString,
resolve: (name) => {
const pages = import.meta.glob("./Pages/**/*.vue", { eager: true });
return pages[`./Pages/${name}.vue`];
},
setup({ App, props, plugin }) {
return createSSRApp({
render: () => h(App, props),
}).use(plugin);
},
})
);
```
### Performance note
With the implementation proposed above, you'll be loading the whole page on the first load.
This is because everything will be bundled in the same file.
If you want to split your code, you can use the following implementation.
`helper.js` (taken from [laravel vite plugin inertia helpers](https://github.com/laravel/vite-plugin/blob/1.x/src/inertia-helpers/index.ts))
```javascript
export async function resolvePageComponent<T>(
path: string | string[],
pages: Record<string, Promise<T> | (() => Promise<T>)>
): Promise<T> {
for (const p of Array.isArray(path) ? path : [path]) {
const page = pages[p];
if (typeof page === "undefined") {
continue;
}
return typeof page === "function" ? page() : page;
}
throw new Error(`Page not found: ${path}`);
}
```
`main.js`
```javascript
import { createApp, h } from "vue";
import { createInertiaApp } from "@inertiajs/vue3";
import { resolvePageComponent } from "@/helper.js";
createInertiaApp({
resolve: (name) => {
return resolvePageComponent(
`./Pages/${name}.vue`,
import.meta.glob("./Pages/**/*.vue")
);
},
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el);
},
});
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hxjo/fastapi-inertia",
"name": "fastapi-inertia",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "inertia, inertiajs, fastapi, python",
"author": "Hugo Mortreux",
"author_email": "70602545+hxjo@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/f4/dc/2b25b842899f0d86b45ffe9b86f0efc2c1a99049966a77b56a9cf7513391/fastapi_inertia-1.0.3.tar.gz",
"platform": null,
"description": "# Inertia.js FastAPI Adapter\n\n<!-- TOC -->\n\n- [Inertia.js FastAPI Adapter](#inertiajs-fastapi-adapter)\n - [Installation](#installation)\n - [Configuration](#configuration)\n - [Examples](#examples)\n - [Usage](#usage)\n - [Create a Jinja2Template](#create-a-jinja2template)\n - [Set up the dependency](#set-up-the-dependency)\n - [Rendering a page](#rendering-a-page)\n - [Rendering assets](#rendering-assets)\n - [Sharing data](#sharing-data)\n - [Flash messages](#flash-messages)\n - [Flash errors](#flash-errors)\n - [Redirect to an external URL](#redirect-to-an-external-url)\n - [Redirect back](#redirect-back)\n - [Enable SSR](#enable-ssr)\n - [Frontend documentation](#frontend-documentation)\n - [For a classic build](#for-a-classic-build)\n - [For a SSR build](#for-a-ssr-build)\n - [Performance note](#performance-note)\n\n## Installation\n\nYou can install the package via pip:\n\n```bash\npip install fastapi-inertia\n```\n\n## Configuration\n\nYou can configure the adapter by passing a `InertiaConfig` object to the `Inertia` class.\nThe following options are available:\n\n| key | default | options | description |\n| ---------------------- | ---------------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |\n| environment | development | development,production | The environment to use |\n| version | 1.0.0 | Any valid string | The version of your server |\n| json_encoder | InertiaJsonEncoder | Any class that extends json.JSONEncoder | The JSON encoder used to encode page data when HTML is returned |\n| manifest_json_path | \"\" | Any valid path | The path to the manifest.json file. Needed in production |\n| dev_url | http://localhost:5173 | Any valid url | The URL to the development server |\n| ssr_url | http://localhost:13714 | Any valid url | The URL to the SSR server |\n| ssr_enabled | False | True,False | Whether to [enable SSR](#enable-ssr). You need to install the `httpx` package, to have set the manifest_json_path and started the SSR server |\n| root_directory | src | Any valid path | The directory in which is located the javascript code in your frontend. Will be used to find the relevant files in your manifest.json. |\n| entrypoint_filename | main.js | Any valid file | The entrypoint for you frontend. Will be used to find the relevant files in your manifest.json. |\n| assets_prefix | \"\" | Any valid string | An optional prefix for your assets. Will prefix the links generated from the assets mentioned in manifest.json. |\n| use_flash_messages | False | True,False | Whether to use [flash messages](#flash-messages). You need to use Starlette's SessionMiddleware to use this feature |\n| flash_message_key | messages | Any valid string | The key to use for [flash errors](#flash-errors) |\n| use_flash_errors | False | True,False | Whether to use flash errors |\n| flash_error_key | errors | Any valid string | The key to use for flash errors |\n| templates | None | A Jinja2Templates instance | The templates instance in which Inertia will look for the `root_template_filename` template |\n| root_template_filename | index.html | Any valid jinja2 template file | The file which will be used to render your inertia application |\n\n## Examples\n\nYou can see different full examples in the `examples` directory\n\n## Usage\n\n### Create a Jinja2Template\n\nIn order to use the Inertia.js adapter, you have to create a Jinja2Template that the library will use.\n\nIt **must** have both an `inertia_head` and an `inertia_body` tag in it.\n\n- `inertia_head` is where the library will place the code that supposedly goes inside the HTML `head` tag\n- `inertia_body` is where the library will place the code that supposedly goes inside the HTML `body` tag\n\nYou can find the simplest example in `inertia/tests/templates/index.html`. \nYou should then register the folder in which you put this file as the directory of your Jinja2Templates\n\n```python\ntemplates = Jinja2Templates(directory=template_dir)\n```\n\nThis option should be passed to the InertiaConfig class presented below, under the `templates` key.\nIf you choose a different template file name than `index.html`, you can also pass the `root_template_filename` key with, as value, your template file name.\n\n### Set up the dependency\n\nThis Inertia.js adapter has been developed to be used as a FastAPI dependency.\nTo use it, you first need to set up the dependency, with your desired configuration.\n\n`inertia_dependency.py`\n\n```python\nfrom fastapi import Depends\nfrom typing import Annotated\nfrom inertia import InertiaConfig, inertia_dependency_factory, Inertia\n\ninertia_config = InertiaConfig(\n # Your desired configuration\n )\n\ninertia_dependency = inertia_dependency_factory(\n inertia_config\n)\n\nInertiaDependency = Annotated[Inertia, Depends(inertia_dependency)]\n```\n\nYou can then access the `InertiaDependency` in your route functions, and use it to render your pages.\n\n### Rendering a page\n\nTo render a page, you can use the `render` method of the `Inertia` class. It takes two arguments:\n\n- The name of the page\n- The data to pass to the page\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\n\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\n\n@app.get('/', response_model=None)\nasync def index(inertia: InertiaDependency) -> InertiaResponse:\n return inertia.render('Index', {\n 'name': 'John Doe'\n })\n```\n\n### Rendering assets\n\nAs your front-end framework likely references assets that are not served by FastAPI,\nyou need to mount a static directory to serve these assets.\n\n`main.py`\n\n```python\nimport os\nfrom fastapi import FastAPI\nfrom fastapi.staticfiles import StaticFiles\nfrom inertia_dependency import inertia_config\n\n\napp = FastAPI()\nwebapp_dir = (\n os.path.join(os.path.dirname(__file__), \"..\", \"webapp\", \"dist\")\n if inertia_config.environment != \"development\"\n else os.path.join(os.path.dirname(__file__), \"..\", \"webapp\", \"src\")\n)\n\napp.mount(\"/src\", StaticFiles(directory=webapp_dir), name=\"src\")\napp.mount(\n \"/assets\", StaticFiles(directory=os.path.join(webapp_dir, \"assets\")), name=\"assets\"\n)\n```\n\n### Sharing data\n\nTo share data, in Inertia, is basically to add data before even entering your route.\nThis is useful, for example, to add a user to all your pages that expects your user to be logged in.\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\n\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\n\ndef current_user(inertia: InertiaDependency):\n inertia.share(user={\n 'name': 'John Doe'\n })\n\n@app.get('/', response_model=None, dependencies=[Depends(current_user)])\nasync def index(inertia: InertiaDependency) -> InertiaResponse:\n \"\"\"\n Because of the dependency, and as we are sharing the user data, the user data will be available in the page.\n \"\"\"\n return inertia.render('Index')\n```\n\n### Flash messages\n\nWith the inertia dependency, you have access to a `flash` helper method that allows you to add flash messages to your pages.\nThis is useful to display messages to the user after a form submission, for example.\nThose messages are called `flash` messages as they are only displayed once. \nYou need to have set `use_flash_messages` to `True` in your configuration to use this feature.\nYou need to have the `SessionMiddleware` enabled in your application to use this feature.\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom starlette.middleware.sessions import SessionMiddleware\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\n\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\napp.add_middleware(SessionMiddleware, secret_key=\"secret\")\n\n\n@app.get('/', response_model=None)\nasync def index(inertia: InertiaDependency) -> InertiaResponse:\n inertia.flash('Index was reached successfully', category='success')\n return inertia.render('Index')\n```\n\n### Flash errors\n\nIf you handle form submissions in your application, and if you do all validation at the pydantic level,\na malformed payload will raise a `RequestValidationError` exception.\nYou can use the `inertia_request_validation_exception_handler` to handle this exception and display the errors to the user.\nIt supports error bags, so you can display multiple errors at once.\nIf the request is not from Inertia, it will fallback to FastAPI's default error handling. \nIn order to use this feature, you need to have set `use_flash_errors` to `True` in your configuration.\nYou also need to have the `SessionMiddleware` enabled in your application to use this feature.\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom pydantic import BaseModel, model_validator\nfrom typing import Any\nfrom fastapi.exceptions import RequestValidationError\nfrom starlette.middleware.sessions import SessionMiddleware\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler, inertia_request_validation_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\n\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\napp.add_exception_handler(RequestValidationError, inertia_request_validation_exception_handler)\napp.add_middleware(SessionMiddleware, secret_key=\"secret\")\n\n\nclass Form(BaseModel):\n name: str\n\n @model_validator(mode=\"before\")\n @classmethod\n def name_must_contain_doe(cls, data: Any):\n if 'Doe' not in data.name:\n raise ValueError('Name must contain Doe')\n\n@app.post('/', response_model=None)\nasync def index(data: Form, inertia: InertiaDependency) -> InertiaResponse:\n return inertia.render('Index')\n```\n\n### Redirect to an external URL\n\nIf you want to redirect the user to an external URL, you can use the `location` method of the `Inertia` class.\nIt takes one argument: the URL to redirect to.\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\n\n@app.get('/', response_model=None)\nasync def index(inertia: InertiaDependency) -> InertiaResponse:\n return inertia.location('https://google.fr')\n```\n\n### Redirect back\n\nIf you want to redirect the user back (for example, after a form submission), you can use the `back` method of the `Inertia` class.\nIt will use the `Referer` header to redirect the user back.\nIf you're on a `GET` request, the status code will be `307`. Otherwise, it will be `303`.\nThat ways, it will trigger a new GET request to the referer URL.\n\n`main.py`\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom inertia import InertiaResponse, InertiaVersionConflictException, inertia_version_conflict_exception_handler\nfrom inertia_dependency import InertiaDependency\n\napp = FastAPI()\napp.add_exception_handler(InertiaVersionConflictException, inertia_version_conflict_exception_handler)\n\n@app.get('/', response_model=None)\nasync def index(inertia: InertiaDependency) -> InertiaResponse:\n return inertia.back()\n```\n\n### Enable SSR\n\nTo enable SSR, you need to set `ssr_enabled` to `True` in your configuration.\nYou also need to have set the `manifest_json_path` to the path of your `manifest.json` file.\nYou need to have the `httpx` package installed to use this feature.\nThis can be done through the following command:\n\n```bash\npip install httpx\n```\n\n## Frontend documentation\n\nThere is no particular caveats to keep in mind when using this adapter.\nHowever, here's an example of how you would set up your frontend to work with this adapter.\n\n### For a classic build\n\n> [!NOTE] \n> To build the project, you can run the `vite build` command\n\n`vite.config.js`\n\n```javascript\nimport { fileURLToPath } from \"node:url\";\nimport { dirname } from \"path\";\n\nimport { defineConfig } from \"vite\";\nimport vue from \"@vitejs/plugin-vue\";\n\nconst projectRoot = dirname(fileURLToPath(import.meta.url));\n// https://vitejs.dev/config/\nexport default defineConfig({\n plugins: [vue()],\n resolve: {\n alias: {\n \"@\": `${projectRoot}/src`,\n },\n },\n build: {\n manifest: \"manifest.json\",\n outDir: \"dist\",\n rollupOptions: {\n input: \"src/main.js\",\n },\n },\n});\n```\n\n`main.js`\n\n```javascript\nimport { createApp, h } from \"vue\";\nimport { createInertiaApp } from \"@inertiajs/vue3\";\n\ncreateInertiaApp({\n resolve: (name) => {\n const pages = import.meta.glob(\"./Pages/**/*.vue\", { eager: true });\n return pages[`./Pages/${name}.vue`];\n },\n setup({ el, App, props, plugin }) {\n createApp({ render: () => h(App, props) })\n .use(plugin)\n .mount(el);\n },\n});\n```\n\n### For a SSR build\n\n> [!NOTE] \n> To build the project, you can run the `vite build` and `vite build --ssr` commands \n> To serve the Inertia SSR server, you can run the `node dist/ssr/ssr.js` command\n\n`vite.config.js`\n\n```javascript\nimport { fileURLToPath } from \"node:url\";\nimport { dirname } from \"path\";\n\nimport { defineConfig } from \"vite\";\nimport vue from \"@vitejs/plugin-vue\";\n\nconst projectRoot = dirname(fileURLToPath(import.meta.url));\n// https://vitejs.dev/config/\nexport default defineConfig(({ isSsrBuild }) => ({\n plugins: [vue()],\n resolve: {\n alias: {\n \"@\": `${projectRoot}/src`,\n },\n },\n build: {\n manifest: isSsrBuild ? false : \"manifest.json\",\n outDir: isSsrBuild ? \"dist/ssr\" : \"dist/client\",\n rollupOptions: {\n input: isSsrBuild ? \"src/ssr.js\" : \"src/main.js\",\n },\n },\n}));\n```\n\n`main.js`\n\n```javascript\nimport { createSSRApp, h } from \"vue\";\nimport { createInertiaApp } from \"@inertiajs/vue3\";\n\ncreateInertiaApp({\n resolve: (name) => {\n const pages = import.meta.glob(\"./Pages/**/*.vue\", { eager: true });\n return pages[`./Pages/${name}.vue`];\n },\n setup({ el, App, props, plugin }) {\n createSSRApp({ render: () => h(App, props) })\n .use(plugin)\n .mount(el);\n },\n});\n```\n\n`ssr.js`\n\n```javascript\nimport { createInertiaApp } from \"@inertiajs/vue3\";\nimport createServer from \"@inertiajs/vue3/server\";\nimport { renderToString } from \"@vue/server-renderer\";\nimport { createSSRApp, h } from \"vue\";\n\ncreateServer((page) =>\n createInertiaApp({\n page,\n render: renderToString,\n resolve: (name) => {\n const pages = import.meta.glob(\"./Pages/**/*.vue\", { eager: true });\n return pages[`./Pages/${name}.vue`];\n },\n setup({ App, props, plugin }) {\n return createSSRApp({\n render: () => h(App, props),\n }).use(plugin);\n },\n })\n);\n```\n\n### Performance note\n\nWith the implementation proposed above, you'll be loading the whole page on the first load.\nThis is because everything will be bundled in the same file.\nIf you want to split your code, you can use the following implementation.\n\n`helper.js` (taken from [laravel vite plugin inertia helpers](https://github.com/laravel/vite-plugin/blob/1.x/src/inertia-helpers/index.ts))\n\n```javascript\nexport async function resolvePageComponent<T>(\n path: string | string[],\n pages: Record<string, Promise<T> | (() => Promise<T>)>\n): Promise<T> {\n for (const p of Array.isArray(path) ? path : [path]) {\n const page = pages[p];\n\n if (typeof page === \"undefined\") {\n continue;\n }\n\n return typeof page === \"function\" ? page() : page;\n }\n\n throw new Error(`Page not found: ${path}`);\n}\n```\n\n`main.js`\n\n```javascript\nimport { createApp, h } from \"vue\";\nimport { createInertiaApp } from \"@inertiajs/vue3\";\nimport { resolvePageComponent } from \"@/helper.js\";\n\ncreateInertiaApp({\n resolve: (name) => {\n return resolvePageComponent(\n `./Pages/${name}.vue`,\n import.meta.glob(\"./Pages/**/*.vue\")\n );\n },\n setup({ el, App, props, plugin }) {\n createApp({ render: () => h(App, props) })\n .use(plugin)\n .mount(el);\n },\n});\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An implementation of the Inertia protocol for FastAPI.",
"version": "1.0.3",
"project_urls": {
"Homepage": "https://github.com/hxjo/fastapi-inertia",
"Repository": "https://github.com/hxjo/fastapi-inertia"
},
"split_keywords": [
"inertia",
" inertiajs",
" fastapi",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "139a54151293ec7f1cbf8fc1b89d9ce3beb48195dadc2091aa8b5bea3dc5d0b8",
"md5": "4203edb2cdfaeea7ef062f1a010d9c12",
"sha256": "784dc07b79dddf586a6d17e82459e5224cc20f80075994c9aed1c9c212fb3650"
},
"downloads": -1,
"filename": "fastapi_inertia-1.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4203edb2cdfaeea7ef062f1a010d9c12",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 14241,
"upload_time": "2024-10-04T17:10:31",
"upload_time_iso_8601": "2024-10-04T17:10:31.282435Z",
"url": "https://files.pythonhosted.org/packages/13/9a/54151293ec7f1cbf8fc1b89d9ce3beb48195dadc2091aa8b5bea3dc5d0b8/fastapi_inertia-1.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f4dc2b25b842899f0d86b45ffe9b86f0efc2c1a99049966a77b56a9cf7513391",
"md5": "c1c626597bd944da5200d53973895919",
"sha256": "d5f3a6ef579b8f0ff431dc5da367d4479f534bc7a8ac7201d958b60ee4b7fba0"
},
"downloads": -1,
"filename": "fastapi_inertia-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "c1c626597bd944da5200d53973895919",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 16156,
"upload_time": "2024-10-04T17:10:32",
"upload_time_iso_8601": "2024-10-04T17:10:32.576172Z",
"url": "https://files.pythonhosted.org/packages/f4/dc/2b25b842899f0d86b45ffe9b86f0efc2c1a99049966a77b56a9cf7513391/fastapi_inertia-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-04 17:10:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hxjo",
"github_project": "fastapi-inertia",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "fastapi-inertia"
}