API Reference¶
This page contains the API reference documentation for the RouteLit FastAPI adapter.
RouteLitFastAPIAdapter¶
routelit_fastapi.adapter.RouteLitFastAPIAdapter(routelit, *, static_path=None, template_path=get_default_template_path(), run_mode='prod', local_frontend_server=None, local_components_server=None, cookie_config=None)
¶
A FastAPI adapter for the RouteLit framework, enabling seamless integration of RouteLit's reactive UI components with FastAPI web applications.
Initialize the RouteLitFastAPIAdapter. - When run_mode="prod", no need to specify local_frontend_server and local_components_server. - When run_mode="dev_client", you need to specify local_frontend_server. - When run_mode="dev_components", you need to specify local_components_server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
routelit
|
RouteLit
|
The RouteLit instance. |
required |
static_path
|
Optional[str]
|
The path to the static js/css assets are. |
None
|
template_path
|
str
|
The path to the index.html template file. Default is in routelit package, so no need to specify. |
get_default_template_path()
|
run_mode
|
RunMode
|
The run mode. Example: "prod", "dev_client", "dev_components". |
'prod'
|
local_frontend_server
|
Optional[str]
|
The local vite frontend server. Example: "http://localhost:5173". |
None
|
local_components_server
|
Optional[str]
|
The local vite components server. Example: "http://localhost:5174". |
None
|
cookie_config
|
Optional[dict[str, Any] | bool]
|
The cookie configuration. Default is production cookie config. Set to |
None
|
Source code in src/routelit_fastapi/adapter.py
configure(app)
¶
Configure the FastAPI application to use the RouteLitFastAPIAdapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application to configure. |
required |
Returns:
| Type | Description |
|---|---|
RouteLitFastAPIAdapter
|
The RouteLitFastAPIAdapter instance. |
Source code in src/routelit_fastapi/adapter.py
configure_static_assets(app, asset_target)
classmethod
¶
Configure static assets for a package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application. |
required |
asset_target
|
AssetTarget
|
The asset target configuration. |
required |
Source code in src/routelit_fastapi/adapter.py
response(view_fn, request, inject_builder=None, *args, **kwargs)
async
¶
Handle a request and return a response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_fn
|
ViewFn
|
The view function to handle the request. |
required |
request
|
Request
|
The FastAPI request. |
required |
inject_builder
|
Optional[bool]
|
Whether to inject the builder into the request. |
None
|
*args
|
Any
|
Additional arguments to pass to the view function. |
()
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the view function. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
A FastAPI response. |
Source code in src/routelit_fastapi/adapter.py
route(path, methods=None)
¶
Decorator to register a route with the adapter and FastAPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The URL path for the route. |
required |
methods
|
list[str] | None
|
List of HTTP methods to allow. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[ViewFn], None]
|
A decorator function. |
Example
@adapter.route("/counter") def counter_view(rl: RouteLitBuilder) -> None: rl.markdown("Hello")
Source code in src/routelit_fastapi/adapter.py
stream_response(view_fn, request, inject_builder=None, *args, **kwargs)
async
¶
Handle a request and return a streaming response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_fn
|
ViewFn
|
The view function to handle the request. |
required |
request
|
Request
|
The FastAPI request. |
required |
inject_builder
|
Optional[bool]
|
Whether to inject the builder into the request. |
None
|
*args
|
Any
|
Additional arguments to pass to the view function. |
()
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the view function. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
A FastAPI streaming response. |
Source code in src/routelit_fastapi/adapter.py
stream_route(path, methods=None)
¶
Decorator to register a streaming route with the adapter and FastAPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The URL path for the route. |
required |
methods
|
list[str] | None
|
List of HTTP methods to allow. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[ViewFn], None]
|
A decorator function. |
Example
@adapter.stream_route("/stream") async def stream_view(rl: RouteLitBuilder) -> None: for i in range(10): rl.text(f"Count: {i}")
Source code in src/routelit_fastapi/adapter.py
handler: python options: members: - init - configure - configure_static_assets - route - stream_route - response - stream_response show_root_heading: true show_source: false heading_level: 3
CookieConfig¶
routelit_fastapi.adapter.CookieConfig
¶
Bases: TypedDict
Configuration for session cookies.
handler: python options: show_root_heading: true show_source: false heading_level: 3
RunMode¶
routelit_fastapi.adapter.RunMode = Literal['prod', 'dev_client', 'dev_components']
module-attribute
¶
The run mode for the RouteLitFastAPIAdapter.
prod: Production mode.dev_client: Development mode for the client.dev_components: Development mode for the components.
handler: python options: show_root_heading: true show_source: false heading_level: 3
RunModeEnum¶
routelit_fastapi.adapter.RunModeEnum
¶
Bases: Enum
handler: python options: show_root_heading: true show_source: false heading_level: 3
FastAPIRLRequest¶
routelit_fastapi.request.FastAPIRLRequest(request)
¶
Bases: RouteLitRequest
Implements the RouteLitRequest interface for FastAPI.
Usage
- Create instance: rl_request = FastAPIRLRequest(request)
- Call build(): await rl_request.build()
- Use with RouteLit handlers: routelit.handle_post_request(..., rl_request, ...)
The build() method must be called before using the request with RouteLit handlers. It pre-processes all async operations (body parsing, file extraction, ui_event parsing).
Source code in src/routelit_fastapi/request.py
fragment_id
property
¶
Get the fragment ID from the request.
Note: build() must be called before accessing this property.
ui_event
property
¶
Get the UI event from the request.
Note: build() must be called before accessing this property.
build()
async
¶
Pre-process all async operations for the request.
This method must be called before using the request with RouteLit handlers. It handles: - JSON body parsing - Multipart form data parsing - File extraction from multipart forms - UI event parsing from the request body
After calling this method, the request is ready to be used with: - routelit.handle_get_request() - routelit.handle_post_request() - routelit.handle_post_request_stream_jsonl()
Source code in src/routelit_fastapi/request.py
get_files()
¶
Get files from request body.
Note: build() must be called before this method.
get_json()
¶
Get JSON data from request body.
Note: build() must be called before this method.
handler: python options: members: - init - build - get_headers - get_path_params - get_referrer - get_json - get_files - is_json - is_multipart - get_query_param - get_query_param_list - get_session_id - get_pathname - get_host - method - ui_event - fragment_id show_root_heading: true show_source: false heading_level: 3
Exports¶
The following are exported from the routelit_fastapi package: