Flask+Vite integration.
Project description
Flask-Vite
A Flask extension that simplifies integration with Vite (a modern frontend build tool).
- Status: Bรชta.
- Free software: MIT license
Flask-Vite bridges the gap between Flask's backend capabilities and modern frontend development tools. It allows you to:
- Use Modern Tools: Leverage Vite's fast development server and optimized builds
- Keep It Simple: Maintain Flask's simplicity while using cutting-edge frontend tools
- Seamless Integration: Automatic asset injection and serving with minimal configuration
- Development & Production: Different behaviors for development (hot reload) and production (optimized assets)
Key Features
๐ Fast Development
- Hot module replacement via Vite dev server
- Instant feedback on frontend changes
- TypeScript, JSX, and modern CSS support
๐ง Simple Configuration
- Minimal setup required
- Automatic asset discovery and injection
- Flask CLI integration for common tasks
๐๏ธ Production Ready
- Optimized builds with code splitting
- Asset fingerprinting for caching
- CDN-friendly asset serving
๐ Framework Agnostic
- Works with vanilla JavaScript, React, Vue, Svelte
- Supports any Vite-compatible framework
- TailwindCSS integration example included
Usage
Instantiate the Flask extension as you do for other Flask extensions:
from flask_vite import Vite
app = Flask(...)
vite = Vite(app)
# or
vite = Vite()
vite.init_app(app)
Then you can use the following commands:
$ flask vite
Usage: flask vite [OPTIONS] COMMAND [ARGS]...
Perform Vite operations.
Options:
--help Show this message and exit.
Commands:
build Build the Vite assets.
check-updates Check outdated Vite dependencies.
init Init the vite/ directory (if it doesn't exist)
install Install the dependencies using npm.
start Start watching source changes for dev.
update Update Vite and its dependencies, if needed.
Example Workflow
This section assumes you have already added Flask-Vite to your Flask app with the steps above.
Step 1: Initialize your /vite subdirectory
# First, create the /vite subdirectory in your Flask app's root folder
$ flask vite init
# Install any dependencies
$ flask vite install
Step 2: Now you are ready to begin development
# Start a local Vite dev server.
# This will hot-reload any changes in the /vite subdirectory, so it's suited for local development.
$ flask vite start
# Make any changes in vite/main.js, such as importing React/Vue components.
# Flask-Vite assumes you have a single entry point at vite/main.js, such as a React SPA (single page application).
You should now be able to see any changes you have made in your Flask app. If not, try Troubleshooting.
Step 3: Ready for production
Once you are ready for production, you need to build your assets.
# Build assets based on /vite/vite.config.js
$ flask vite build
You should now see files like /vite/dist/assets/index-f16ca036.js.
If you are running your Flask app in production mode (ie without app.debug), you should see these asset files included in your Flask Jinja templates automatically. If not, try Troubleshooting.
Features
- Manages a
vitedirectory where you put your front-end source code. - Auto-injects vite-generated assets into your HTML pages (if
VITE_AUTO_INSERTis set in the Flask config). - Use
{{ vite_tags() }}in your Jinja templates otherwise. - If you run Flask in
host_matchingmode, you can tell Vite which host to mount its own views on. You can configure this when instantiating Vite or when callinginit_app:- Pass
vite_routes_hostthe specific single host to serve its assets from. - Pass
vite_routes_hostas the wildcard value*to serve vite assets from the same domain as the current request.
- Pass
Configuration
The following (Flask) configuration variables are available:
VITE_AUTO_INSERT: if set, the extension will auto-insert the Vite assets into your HTML pages.VITE_NPM_BIN_PATH: path to thenpmbinary. Defaults tonpm.VITE_FOLDER_PATH: path for the vite project. Defaults tovitelocally.
Demo
See the demo/ directory for a working demo using TailwindCSS.
Architecture
Flask-Vite works differently in development and production:
Development Mode
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Flask App โ โ Vite Server โ
โ :5000 โ โ :3000 โ
โ โ โ โ
โ {{ vite_ โโโโโถโ Hot Reload โ
โ tags() }}โ โ Assets โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
Production Mode
โโโโโโโโโโโโโโโ
โ Flask App โ
โ :5000 โ
โ โ
โ {{ vite_ โโโโโถ Built Assets
โ tags() }}โ in /dist
โโโโโโโโโโโโโโโ
Project Structure
A typical Flask-Vite project looks like:
my-flask-app/
โโโ app.py # Flask application
โโโ templates/ # Jinja2 templates
โ โโโ index.html # Uses {{ vite_tags() }}
โโโ vite/ # Frontend source
โ โโโ package.json # Node dependencies
โ โโโ vite.config.js # Vite configuration
โ โโโ main.js # Entry point
โ โโโ src/ # Source files
โ โโโ dist/ # Built assets (production)
โโโ requirements.txt # Python dependencies
Documentation
๐ User Guide
Complete guide for using Flask-Vite in your applications, including installation, configuration, development workflow, and troubleshooting.
๐ง Developer Guide
In-depth guide for contributing to Flask-Vite development, including architecture overview, testing, and contribution guidelines.
๐ API Reference
Complete API documentation with all classes, methods, configuration options, and CLI commands.
Quick Links
Getting Started
Examples
Troubleshooting
I can't see my vite output files (eg React/Vue components) in my Jinja templates
- Flask-Vite will automatically add these files to your templates if you either:
- set
VITE_AUTO_INSERT=Truein your Flask config - OR, explicitly include
{{ vite_tags() }}somewhere in your Jinja templates
- set
Either of these options will insert <script> tags into your Jinja templates, which will be the output of your vite config.
Script tags are included in my Jinja templates, but they're not loading
- If your Flask app is running in debug mode (ie app.debug):
- your HTML should have a line like
<script type="module" src="http://localhost:3000/main.js"></script> - If this file isn't loading it's because your local Vite dev server isn't running. Start it by using
flask vite start
- your HTML should have a line like
- If your Flask app is running in production mode (ie not app.debug):
- your HTML should have a line like
<script type="module" src="/_vite/index-f16ca036.js"></script>(the hash inindex-[hash].jswill change every time) - you should find this file in
/vite/dist/assets/index-f16ca036.js. If not, you can build for production again usingflask vite build
- your HTML should have a line like
Support and Community
- GitHub Repository: abilian/flask-vite
- Issues: Report bugs and request features on GitHub
- License: MIT License
Contributing
We welcome contributions! See the Developer Guide for:
- Development setup instructions
- Code style guidelines
- Testing procedures
- Pull request process
Credits
This project is inspired by the
Django-Tailwind project and was previously known as Flask-Tailwind.
This package was created with Cookiecutter, using the abilian/cookiecutter-abilian-python project template.
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-0.6.1.tar.gz.
File metadata
- Download URL: flask_vite-0.6.1.tar.gz
- Upload date:
- Size: 50.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1681765668fa9dc7ca985f5ee395b1f7ed1d4db861407cccbcfdaf0346d4112c
|
|
| MD5 |
bc8ff594fc65ecf275a52239161d36a7
|
|
| BLAKE2b-256 |
b946519974e78a45b05822fc3505d7b0fed02f1145fdc24bd955b091c29d5ccf
|
File details
Details for the file flask_vite-0.6.1-py3-none-any.whl.
File metadata
- Download URL: flask_vite-0.6.1-py3-none-any.whl
- Upload date:
- Size: 49.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5267abdd0bb4c8c9bb5b1a44b1260dc015a3f7e39befcf120babedc386d43558
|
|
| MD5 |
f4c682eb82f9f0ee9622990669fee79a
|
|
| BLAKE2b-256 |
de9a6dc7d80acd2c5ecf57d563f925f03cffe3ffb2f400d280eacab2ecf4a94e
|