Skip to main content

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

templates = Jinja2Templates(directory='templates')
templates.env.globals['vite_hmr_client'] = fastapi_vite.vite_hmr_client
templates.env.globals['vite_asset'] = fastapi_vite.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>

Release files for fastapi-vite 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-vite 0.3.2
File Size Uploaded
fastapi_vite-0.3.2.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-vite 0.3.2
File Interpreter ABI Platform
fastapi_vite-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / fastapi_vite-0.3.2.tar.gz

Download URL fastapi_vite-0.3.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8592a4de024831a016dac5f245ad4c69d0cd5787125881a9a1681c601514f561
BLAKE2b-256 checksum
How to use checksums
5d7abfc2cacb00e9d0386a3c520bc62a5e8623499f13567c4b7269b15c7b7a2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.9 Darwin/21.2.0

Release files / fastapi_vite-0.3.2-py3-none-any.whl

Download URL fastapi_vite-0.3.2-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73378b36ad75acd37ba369879b0af7dbdad21589ec0b6031d7a1a6af88944291
BLAKE2b-256 checksum
How to use checksums
b315ba62e56f5c7a331b72e230d1f7bc83f05cb1c05b1fb1aee41cf2ef34bb84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.9 Darwin/21.2.0

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page