Flask extension for serving Vite-managed assets in development and production
Project description
Flask-Vite-Assets
A Flask extension that seamlessly integrates Vite asset management into your Flask/Jinja2 templates, supporting both the Vite dev server (with HMR) and production manifest-based asset fingerprinting.
Features
- 🔥 Hot Module Replacement (HMR) in development via the Vite dev server
- 📦 Manifest-based asset fingerprinting in production
- 🎨 Automatic CSS injection from the Vite manifest
- ⚡ Cache-busting
url_for— static file URLs automatically gain a?v=<mtime>parameter so browsers always pick up the latest file after a deployment - 🔌 Flask extension pattern with
init_appsupport - 🧩 Zero-config defaults with full configurability
- 🐍 Typed — ships with
py.typed
Installation
pip install flask-vite-assets
If you use the Vite dev server in development, also install requests:
pip install "flask-vite-assets[dev-server]"
Quick start
Application factory pattern
from flask import Flask
from flask_vite_assets import ViteAssets
vite = ViteAssets()
def create_app():
app = Flask(__name__)
vite.init_app(app)
return app
Direct initialisation
from flask import Flask
from flask_vite_assets import ViteAssets
app = Flask(__name__)
ViteAssets(app)
Jinja2 templates
<!DOCTYPE html>
<html>
<head>
<!-- Injects <link> + <script> tags from the Vite manifest in production,
or a <script> tag pointing at the Vite dev server in development -->
{{ vite_asset("js/main.js") }}
<!-- Cache-busting url_for: appends ?v=<mtime> to static file URLs so
browsers always fetch the latest file after a deployment -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/extra.css') }}">
</head>
<body>
...
<!-- Injects the Vite HMR client in development only (no-op in production) -->
{{ vite_hmr_client() }}
</body>
</html>
How it works
Development mode (DEBUG=True)
- The extension attempts to connect to the Vite dev server (default
http://localhost:5173). - If the server is reachable it returns a
<script type="module">tag pointing at the dev server, enabling HMR. - If the primary port is unavailable it automatically tries the next port (
5174), which Vite uses when the preferred port is already occupied. - If neither server is reachable it falls back to the production manifest.
Production mode (DEBUG=False)
- Reads
<app.static_folder>/.vite/manifest.json. - For each entry emits
<link rel="stylesheet">tags for associated CSS files, then a<script type="module">tag for the JS bundle. - Falls back to serving the asset path directly if the manifest is missing or malformed.
Configuration
All configuration is optional.
| Flask config key | Environment variable | Default | Description |
|---|---|---|---|
VITE_DEV_SERVER |
VITE_DEV_SERVER |
http://localhost:5173 |
Base URL of the Vite dev server |
VITE_MANIFEST_PATH |
— | <app.static_folder>/.vite/manifest.json |
Absolute path to the production Vite manifest |
Flask config takes precedence over environment variables.
Vite configuration
Point Vite's output at your Flask static folder and enable manifest generation:
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
root: "resources",
base: "/static/",
build: {
outDir: "../resources/static",
manifest: true,
rollupOptions: {
input: "assets/js/main.js",
},
},
});
Using the helpers directly
You can also call the helpers directly in Python code instead of going through the extension:
from flask_vite_assets import vite_asset, vite_hmr_client
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 flask_vite_assets-0.2.0.tar.gz.
File metadata
- Download URL: flask_vite_assets-0.2.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa9ed7923c9f25c0e7baaf5c7023e7250399ac7d7052246356e247264d3ae019
|
|
| MD5 |
e02b87ec49e042291bfcf205721ee19f
|
|
| BLAKE2b-256 |
9adefd7571f3147583bdd9b7e938229fcb85aaf46ad081c318e30e9d5c8e6fd4
|
Provenance
The following attestation bundles were made for flask_vite_assets-0.2.0.tar.gz:
Publisher:
release.yml on mrailton/flask-vite-assets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_vite_assets-0.2.0.tar.gz -
Subject digest:
aa9ed7923c9f25c0e7baaf5c7023e7250399ac7d7052246356e247264d3ae019 - Sigstore transparency entry: 986790484
- Sigstore integration time:
-
Permalink:
mrailton/flask-vite-assets@106408a8ec69a054e8f6e7cd8732856f94088b9f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/mrailton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@106408a8ec69a054e8f6e7cd8732856f94088b9f -
Trigger Event:
push
-
Statement type:
File details
Details for the file flask_vite_assets-0.2.0-py3-none-any.whl.
File metadata
- Download URL: flask_vite_assets-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.7 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 |
c508563a25c5974a44293af5089ebd9c9e700826d2cc7c4c8e0f65bda341439a
|
|
| MD5 |
98d3b62fe3f4c592f423de666e15f13c
|
|
| BLAKE2b-256 |
72e6c1f5d0d36e0318364c609b81c5929f926092a5e5a06a0d4df01f5e8541b9
|
Provenance
The following attestation bundles were made for flask_vite_assets-0.2.0-py3-none-any.whl:
Publisher:
release.yml on mrailton/flask-vite-assets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_vite_assets-0.2.0-py3-none-any.whl -
Subject digest:
c508563a25c5974a44293af5089ebd9c9e700826d2cc7c4c8e0f65bda341439a - Sigstore transparency entry: 986790504
- Sigstore integration time:
-
Permalink:
mrailton/flask-vite-assets@106408a8ec69a054e8f6e7cd8732856f94088b9f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/mrailton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@106408a8ec69a054e8f6e7cd8732856f94088b9f -
Trigger Event:
push
-
Statement type: