Skip to main content

A FastAPI adapter for the RouteLit framework, enabling seamless integration of RouteLit's reactive UI components with FastAPI web applications

Project description

RouteLit FastAPI Adapter

Release Build status codecov Commit activity License

Routelit

A FastAPI adapter for the RouteLit framework, enabling seamless integration of RouteLit's reactive UI components with FastAPI web applications.

Installation

pip install routelit routelit-fastapi

Usage

from fastapi import FastAPI, Request
from routelit import RouteLit, RouteLitBuilder
from routelit_fastapi import RouteLitFastAPIAdapter

app = FastAPI()
routelit = RouteLit()
routelit_adapter = RouteLitFastAPIAdapter(routelit).configure(app)


def build_index_view(rl: RouteLitBuilder):
    rl.text("Hello, World!")


# Option 1: Traditional approach with explicit route registration
@app.api_route("/", methods=["GET", "POST"])
async def index(request: Request):
    return await routelit_adapter.response(build_index_view, request)


# Option 2: Simplified decorator approach
@routelit_adapter.route("/")
def index_view(rl: RouteLitBuilder):
    rl.text("Hello, World!")

Quick Start

Here's a complete example:

from fastapi import FastAPI, Request
import uvicorn
from routelit import RouteLit, RouteLitBuilder
from routelit_fastapi import RouteLitFastAPIAdapter

# Create FastAPI app
app = FastAPI()

# Create RouteLit instance
routelit = RouteLit()

# Create and configure the adapter
adapter = RouteLitFastAPIAdapter(routelit).configure(app)

# Define your view function
def build_hello_view(rl: RouteLitBuilder):
    name = rl.text_input(label="What is your name", placeholder="John", default_value="")
    rl.write(f"Hello {name}")
    if rl.button("Submit"):
        rl.write("Thanks for your submission!")

# Option 1: Traditional approach
@app.api_route("/", methods=["GET", "POST"])
async def hello(request: Request):
    return await adapter.response(build_hello_view, request)


# Option 2: Simplified decorator approach (recommended)
@adapter.route("/")
def hello_view(rl: RouteLitBuilder):
    name = rl.text_input(label="What is your name", placeholder="John", default_value="")
    rl.write(f"Hello {name}")
    if rl.button("Submit"):
        rl.write("Thanks for your submission!")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

Development Mode

For development with hot reloading of the frontend:

adapter = RouteLitFastAPIAdapter(
    routelit,
    run_mode="dev_client",
    local_frontend_server="http://localhost:5173"
).configure(app)

Streaming Support

For HTTP streaming responses (using JSONL streaming):

# Option 1: Traditional approach
@app.api_route("/stream", methods=["GET", "POST"])
async def stream_endpoint(request: Request):
    return await adapter.stream_response(build_view, request)


# Option 2: Simplified decorator approach (recommended)
@adapter.stream_route("/stream")
async def stream_view(rl: RouteLitBuilder):
    for i in range(10):
        rl.text(f"Count: {i}")

Configuration Options

  • static_path: Custom path for static assets
  • template_path: Custom path for templates
  • run_mode: One of "prod", "dev_client", "dev_components"
  • local_frontend_server: URL of local Vite dev server (for dev mode)
  • local_components_server: URL of local components dev server (for dev mode)
  • cookie_config: Custom cookie configuration

Maintained by @rolangom.


Repository initiated with fpgmaas/cookiecutter-uv.

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

routelit_fastapi-0.0.1.tar.gz (105.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

routelit_fastapi-0.0.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file routelit_fastapi-0.0.1.tar.gz.

File metadata

  • Download URL: routelit_fastapi-0.0.1.tar.gz
  • Upload date:
  • Size: 105.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for routelit_fastapi-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4d7695e8d4073e7f5c6868c73cd16679a3e7bf9fa4170ca1878b10abb5adea48
MD5 1d99a4cdc3170ec71cf9df1fe7f9bf8d
BLAKE2b-256 c92ffee45868e37a27250f5d16eeefebef0bb759f92723705e6039505f120770

See more details on using hashes here.

File details

Details for the file routelit_fastapi-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: routelit_fastapi-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for routelit_fastapi-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f89625c08e1b2e70207fff18623b7a73f21e5cf722de3699c0239a81c2fc1b25
MD5 b77d66f44e3997633c90c84167742417
BLAKE2b-256 5377dc914de4a73059e30e51689ddd202fca7c022ca12b725631af2ec6de5e06

See more details on using hashes here.

Supported by

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