LazyFast is a lightweight Python library designed for building modern web interfaces using a component-based approach. It enables writing page logic on the server side in Python, integrating seamlessly with FastAPI.
MIT License
LazyFast is a lightweight Python library designed for building modern web interfaces using a component-based approach. It enables writing page logic on the server side in Python, integrating seamlessly with FastAPI. With LazyFast, interactive elements like inputs, buttons, and selects trigger component reloads that occur on the server, updating the component's state dynamically.
To install LazyFast, use pip:
pip install lazyfast
or
poetry add lazyfast
Here's an example application to demonstrate how LazyFast works:
from fastapi import FastAPI, Request
from lazyfast import LazyFastRouter, Component, tags
# LazyFastRouter inherits from FastAPI's APIRouter
router = LazyFastRouter()
# Define a lazy-loaded HTML component powered by HTMX
@router.component()
class MyComponent(Component):
title: str
async def view(self, request: Request) -> None:
tags.h1(self.title, class_="my-class")
with tags.div(style="border: 1px solid black"):
tags.span(request.headers)
# Initialize the page dependencies for component rendering
# The page endpoint is also a FastAPI endpoint
@router.page("/{name}")
def root(name: str):
with tags.div(class_="container mt-6"):
MyComponent(title=f"Hello, World from {name}")
# Embed the router in a FastAPI app
app = FastAPI()
app.include_router(router)
If you use uvicorn
instead as a server and want to reload on changes, use the following command:
uvicorn app:app --reload --timeout-graceful-shutdown 1
Documentation can be found here.
LazyFast is licensed under the MIT License.