Skip to main content

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: from dev_server_url)
  • VITE_PORT - Override Vite dev server port (default: from dev_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


Download files

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

Source Distribution

fastapi_vite_assets-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

fastapi_vite_assets-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for fastapi_vite_assets-0.1.0.tar.gz
Algorithm Hash digest
SHA256 52393702d4ee88d75086270318fec6207e4c64b5a0d354cf2109940c149adb50
MD5 409d17956a1de3655c2b61fa09839c0e
BLAKE2b-256 3adbfe327e0e78ae6a99b7585ef1d3c5c65a37dcd2299ddd96d5cc86402ab150

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_vite_assets-0.1.0.tar.gz:

Publisher: publish.yml on jkupcho/fastapi-vite-assets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_vite_assets-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_vite_assets-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c862407ba961c5d04878509a772da3c9ecd0e7fa45afda8201aaf0d92b10ae62
MD5 80ad61db20380c9edaaa0ddafd369c72
BLAKE2b-256 4a81931f6c8c97e50ab736e37e13c58c387b6bbb6f1ff78467c33daf46aca71a

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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