Skip to main content

Rizzler creates a parallel front-end dev-server using ViteJS for ASGI Frameworks

Project description

Rizzler

Package vesion Format Python version License Code size Top Languages Repository size Last commit Rizzler Banner

Installation

Install using pip

$ pip install rizzler
> ...

Usage

Integrate with lifespan protocol.

from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.requests impor Request
from fastapi.responses import HTMLResponse
from rizzler import RizzleTemplates, Rizzler
from typing import AsyncIterator, List, Tuple

@Rizzler.load_config
def rizzler_settings() -> List[Tuple[str, str]]:
  return [
    ("command", "pnpm"),
    ("framework", "vue")
  ]

@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None, None]:
  await Rizzler.serve()
  yield
  Rizzler.shutdown()

app: FastAPI = FastAPI(lifespan=lifespan)
templates: RizzleTemplates = RizzleTemplates(directory="templates")

@app.get("/", response_class=HTMLResponse)
async def index(request: Request) -> HTMLResponse:
  return templates.TemplateResponse("index.html", {"request": request})

Templating

RizzleTemplates is an extension on top of Jinja2Templates class found under starlette However, has two overriding methods that must be placed inside the template HTML-file as such:

<!DOCTYPE html>
<html>
  <head><!-- ... --></head>
  <body>
    {{ vite_hmr_client() }}
    {{ vite_asset('pages/main.js') }}
  </body>
</html>

Build

You can run the following command once you are done customizing the front-end code under pages/ directory to your liking.

rzl build
Example outputs for `rzl build`
$ rzl build
> INFO     ⚡Building Rizzler front-end…
> INFO
> INFO     > rzl-tmp@0.0.0 build /Users/mackasitt/workspaces/rzl-react
> INFO     > vite build                                       
> INFO                                                       
> INFO     vite v5.3.3 building for production...           
> INFO     transforming...                                 
> INFO      32 modules transformed.                      
> INFO     rendering chunks...                           
> INFO     computing gzip size...                       
> INFO     dist/rizz.svg    4.13 kB  gzip:  2.14 kB   
> INFO     dist/rizz.css    1.39 kB  gzip:  0.72 kB  
> INFO     dist/rizz.js   142.63 kB  gzip: 45.74 kB 
> INFO      built in 390ms

Now you can stop using RizzleTemplates and revert back to serving front-end with Jinja2Templates as such

#!/usr/bin/env python3
from fastapi import FastAPI
from fastapi.requests import Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="dist")

@app.get("/", response_class=HTMLResponse)
def index(request: Request) -> HTMLResponse:
  return templates.TemplateResponse("index.html", {"request": request})

app.mount("/", StaticFiles(directory="dist"), name="dist")

Voila! Now you have a production front-end to go with your FastAPI application when you need. There will probably be bugs when it comes to relative versus absolute paths in the future. But this is good enough for many prototyping use-case and with a bit of tinkering, can replace

Contributions

To be determined.

Acknowledgements

License

This project is licensed under the terms of the MIT license.

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

rizzler-0.1.9.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

rizzler-0.1.9-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file rizzler-0.1.9.tar.gz.

File metadata

  • Download URL: rizzler-0.1.9.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.0 Darwin/22.1.0

File hashes

Hashes for rizzler-0.1.9.tar.gz
Algorithm Hash digest
SHA256 49c72300bf023d5db19a2e256c3df79c16ef59bc8b2d732235d4fd9ddd517fd8
MD5 b43c4a19b1df77a50c2765732188e709
BLAKE2b-256 b02fa9f990ad6b5a4730c5b31716a439b54b7376e9c4d37fafbb51b0c818563e

See more details on using hashes here.

File details

Details for the file rizzler-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: rizzler-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.0 Darwin/22.1.0

File hashes

Hashes for rizzler-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 122672a5142e4ffa6c05e581fed414f379dbe37358aa3abc0999e2dbf82fc4d1
MD5 9666fcea2f421899f3ed0f1b6635f60a
BLAKE2b-256 42f175be8ce23a472b1bf204de53c4d2029d6e82c87947e10bee7fcd1ac3bc87

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