Skip to main content

A jinja2 view template helping function for FastAPI.

Project description

fastapi-view

A jinja2 view template helping function for FastAPI.

Features:

  • Simply setting and use function to return Jinja2Templates

Installation

pip install fastapi-view

Usage

  • Jinja2 templates directory setup.

    import os
    from contextlib import asynccontextmanager
    
    from fastapi import FastAPI
    from fastapi.staticfiles import StaticFiles
    from fastapi.templating import Jinja2Templates
    
    from fastapi_view import view
    from fastapi_view.middleware import ViewRequestMiddleware
    
    @asynccontextmanager
    async def lifespan(app: FastAPI):
        view.initialize(Jinja2Templates(directory="./assets/views"), use_vite=True)
        yield
    
    app = FastAPI(title="Test app", lifespan=lifespan)
    
  • Use view() to render Jinja2 *.html files.

    @app.get("/")
    def index():
        # index.html in ./assets/views
        return view("index", {"foo": "bar"})
    
  • Use inertia.render() to render *.vue files.

    from fastapi_view import inertia
    
    @app.get("/inertia/page")
    def inertia_index():
        # Index.vue in ./assets/js/Pages/views
        return inertia.render("Index", props={"foo": "bar"})
    
  • Find more examples in example directory.

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_view-0.4.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

fastapi_view-0.4.0-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

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