Skip to main content

Integration utilities for FastAPI and ViteJS.

Project description

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>

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-0.3.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

fastapi_vite-0.3.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_vite-0.3.2.tar.gz.

File metadata

  • Download URL: fastapi_vite-0.3.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.9 Darwin/21.2.0

File hashes

Hashes for fastapi_vite-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8592a4de024831a016dac5f245ad4c69d0cd5787125881a9a1681c601514f561
MD5 6cc3fb943b8dd19d5bd3ac0c654b36bc
BLAKE2b-256 5d7abfc2cacb00e9d0386a3c520bc62a5e8623499f13567c4b7269b15c7b7a2b

See more details on using hashes here.

File details

Details for the file fastapi_vite-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_vite-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.9 Darwin/21.2.0

File hashes

Hashes for fastapi_vite-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 73378b36ad75acd37ba369879b0af7dbdad21589ec0b6031d7a1a6af88944291
MD5 1186a0be492c1fc7a8d2b7dc0f460876
BLAKE2b-256 b315ba62e56f5c7a331b72e230d1f7bc83f05cb1c05b1fb1aee41cf2ef34bb84

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page