fastapi-vite
Integration for FastAPI and Vite JS
what?
This package is designed to make working with javascript assets easier.
fastapi-vite enables the jinja filters required to render asset URLs to jinja templates
Inspired by django-vite @ [https://github.com/MrBin99/django-vite]
installation
Install using pip
pip install fastapi-vite
Usage
Configure Jinja templating for FastAPI
import fastapi_vite_dara
templates = Jinja2Templates(directory='templates')
templates.env.globals['vite_hmr_client'] = fastapi_vite_dara.vite_hmr_client
templates.env.globals['vite_asset'] = fastapi_vite_dara.vite_asset
Configure Vite
Here is an example used to test this plugin
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
const Dotenv = require("dotenv");
import path from "path";
Dotenv.config({ path: path.join(__dirname, ".env") });
const STATIC_URL = process.env.STATIC_URL;
// https://vitejs.dev/config/
export default defineConfig({
base: `${STATIC_URL}`,
clearScreen: false,
plugins: [
reactRefresh(),
],
build: {
target: "esnext",
outDir: "./static/",
emptyOutDir: true,
assetsDir: "",
manifest: true,
rollupOptions: {
input: "./assets/javascript/main.tsx"
},
},
root: ".", // You can change the root path as you wish
})
Configure Static Assets
Configure Templates
*render_vite_hmr no-op when in production.
{{ render_vite_hmr_client() }}
<script
type="text/javascript"
defer
src="{{ asset_url('javascript/main.tsx') }}"
></script>
Metadata
Release files for fastapi_vite_dara 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_vite_dara-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / fastapi_vite_dara-0.4.0-py3-none-any.whl
| Download URL | fastapi_vite_dara-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06c6e8e773801c142828796d98e2cb61c70a9f7703f97858568c5c4f81e79ede
|
|
BLAKE2b-256 checksum How to use checksums |
7daa56e7a003bef6d4c6527c23c7a920abb862af383999250259424786df88a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.11.10 Darwin/24.1.0
|