Vite asset management for FastAPI with Jinja2 templates
Project description
FastAPI Vite Integration
Seamless Vite asset management for FastAPI applications with Jinja2 templates.
Features
- 🔥 Hot Module Replacement (HMR) in development
- 📦 Automatic manifest parsing for production builds
- 🎯 Simple API - one function call to setup
- ⚡ Fast - leverages Vite's speed in development
- 🔧 Configurable - customize paths and behavior
- 🎨 Framework agnostic - works with any Vite frontend setup
Installation
# Using uv
uv add fastapi-vite-assets
# Using pip
pip install fastapi-vite-assets
Quick Start
1. Configure Vite
In your vite.config.ts:
import { defineConfig } from "vite";
export default defineConfig({
build: {
manifest: true,
rollupOptions: {
input: ["src/main.ts", "src/style.css"],
},
},
});
2. Setup FastAPI
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi_vite_assets import ViteConfig, setup_vite
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# Configure Vite integration
vite_config = ViteConfig(
assets_path="web/dist",
manifest_path="web/dist/.vite/manifest.json",
)
setup_vite(app, templates, vite_config)
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
3. Create Templates
In templates/base.html:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
{{ vite_hmr_client() }}
{{ vite_asset("src/style.css") }}
</head>
<body>
{% block content %}{% endblock %}
{{ vite_asset("src/main.ts") }}
</body>
</html>
Configuration
ViteConfig Options
ViteConfig(
# Path to Vite build output directory
assets_path: str = "dist",
# Path to Vite manifest.json
manifest_path: str = "dist/.vite/manifest.json",
# Vite dev server URL
dev_server_url: str = "http://localhost:5173",
# URL prefix for static assets in production
static_url_prefix: str = "/static",
# Auto-detect dev mode from ENV variable
auto_detect_dev: bool = True,
# Force dev/prod mode (overrides auto-detection)
force_dev_mode: Optional[bool] = None,
# Base path to resolve relative paths
base_path: Optional[Path] = None,
)
Environment Variables
ENV- Set to"production"for production mode (default:"development")VITE_HOST- Override Vite dev server host (default: fromdev_server_url)VITE_PORT- Override Vite dev server port (default: fromdev_server_url)
Template Functions
vite_hmr_client()
Injects the Vite HMR client script tag in development mode. Does nothing in production.
{{ vite_hmr_client() }}
vite_asset(path)
Injects the appropriate asset tag(s) for the given entry point.
Development mode: Points to Vite dev server
<script type="module" src="http://localhost:5173/src/main.ts"></script>
Production mode: Reads from manifest and includes all dependencies
<script type="module" src="/static/assets/main-abc123.js"></script>
<link rel="stylesheet" href="/static/assets/main-def456.css">
Development vs Production
Development
# Terminal 1 - Start Vite dev server
cd web && npm run dev
# Terminal 2 - Start FastAPI
fastapi dev app/main.py
Production
# Build Vite assets
cd web && npm run build
# Run FastAPI with production environment
ENV=production uvicorn app.main:app
Docker Deployment
See the example Dockerfile in the repository for a multistage build setup.
Examples
Check out the packages/example directory for a complete working example.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_vite_assets-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_vite_assets-0.1.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52393702d4ee88d75086270318fec6207e4c64b5a0d354cf2109940c149adb50
|
|
| MD5 |
409d17956a1de3655c2b61fa09839c0e
|
|
| BLAKE2b-256 |
3adbfe327e0e78ae6a99b7585ef1d3c5c65a37dcd2299ddd96d5cc86402ab150
|
Provenance
The following attestation bundles were made for fastapi_vite_assets-0.1.0.tar.gz:
Publisher:
publish.yml on jkupcho/fastapi-vite-assets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_vite_assets-0.1.0.tar.gz -
Subject digest:
52393702d4ee88d75086270318fec6207e4c64b5a0d354cf2109940c149adb50 - Sigstore transparency entry: 732161656
- Sigstore integration time:
-
Permalink:
jkupcho/fastapi-vite-assets@1bdb6117908cbbe6d248fce5dee4f51982720a4f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/jkupcho
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1bdb6117908cbbe6d248fce5dee4f51982720a4f -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_vite_assets-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_vite_assets-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c862407ba961c5d04878509a772da3c9ecd0e7fa45afda8201aaf0d92b10ae62
|
|
| MD5 |
80ad61db20380c9edaaa0ddafd369c72
|
|
| BLAKE2b-256 |
4a81931f6c8c97e50ab736e37e13c58c387b6bbb6f1ff78467c33daf46aca71a
|
Provenance
The following attestation bundles were made for fastapi_vite_assets-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on jkupcho/fastapi-vite-assets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_vite_assets-0.1.0-py3-none-any.whl -
Subject digest:
c862407ba961c5d04878509a772da3c9ecd0e7fa45afda8201aaf0d92b10ae62 - Sigstore transparency entry: 732161657
- Sigstore integration time:
-
Permalink:
jkupcho/fastapi-vite-assets@1bdb6117908cbbe6d248fce5dee4f51982720a4f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/jkupcho
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1bdb6117908cbbe6d248fce5dee4f51982720a4f -
Trigger Event:
release
-
Statement type: