App
reflex.app.App
The main Reflex app that encapsulates the backend and frontend.
Every Reflex app needs an app defined in its main module.
# app.py
import reflex as rx
# Define state and pages
...
app = rx.App(
# Set global level style.
style={...},
# Deprecated legacy shortcut for the Radix Themes plugin.
theme=rx.theme(accent_color="blue"),
)Fields
theme
Optional[Component]Default: None
Deprecated legacy shortcut for configuring the app-level Radix theme.
style
dict[str | type[BaseComponent] | Callable | ComponentNamespace, Any]Default: {}
The global style for the app.
stylesheets
list[str]Default: []
A list of URLs to stylesheets to include in the app.
reset_style
boolDefault: True
Whether to include CSS reset for margin and padding. Defaults to True.
app_wraps
dict[tuple[int, str], Callable[[bool], Optional[Component]]]App wraps to be applied to the whole app. Expected to be a dictionary of (order, name) to a function that takes whether the state is enabled and optionally returns a component.
extra_app_wraps
dict[tuple[int, str], Callable[[bool], Optional[Component]]]Default: {}
Extra app wraps to be applied to the whole app.
head_components
list[Component]Default: []
Components to add to the head of every page.
sio
Optional[AsyncServer]Default: None
The Socket.IO AsyncServer instance.
html_lang
Optional[str]Default: None
The language to add to the html root tag of every page.
html_custom_attrs
Optional[dict[str, str]]Default: None
Attributes to add to the html root tag of every page.
enable_state
boolDefault: True
Whether to enable state for the app. If False, the app will not use state.
admin_dash
Optional[AdminDash]Default: None
Admin dashboard to view and manage the database.
frontend_exception_handler
Callable[[Exception], None]Default: default_frontend_exception_handler
Frontend error handler function.
backend_exception_handler
Callable[[Exception], Optional[EventSpec | list[EventSpec]]]Default: default_backend_exception_handler
Backend error handler function.
hydrate_fallback
Optional[Component | Callable[[], Component | tuple[Component, Ellipsis] | str]]Default: None
api_transformer
Optional[Sequence[Callable[[ASGIApp], ASGIApp] | Starlette] | Callable[[ASGIApp], ASGIApp] | Starlette]Default: None
One or more transforms applied to the backend ASGI app before it runs — mount a FastAPI/Starlette app or wrap it in ASGI middleware. See the API Transformer docs for examples.
Methods
set_contexts
set_contexts() -> AbstractContextManagerSet Reflex contexts needed for state and event processing.
Pushes RegistrationContext and EventContext into the current contextvars scope, but only if they are not already set.
Can be used as a context manager::
with app.set_contexts():
async with app.modify_state(token) as state:
...Returns
A context manager that resets any contexts that were set on exit.
add_page
add_page(
component: Optional[Component | ComponentCallable] = None,
route: Optional[str] = None,
title: Optional[str | Var] = None,
description: Optional[str | Var] = None,
image: str = 'favicon.ico',
on_load: Optional[EventType[()]] = None,
meta: Sequence[Mapping[str, Any] | Component] = [],
context: Optional[dict[str, Any]] = None,
)Add a page to the app.
If the component is a callable, by default the route is the name of the function. Otherwise, a route must be provided.
Parameters
component
The component to display at the page.
route
The route to display the component at.
title
The title of the page.
description
The description of the page.
image
The image to display on the page.
on_load
The event handler(s) that will be called each time the page load.
meta
The metadata of the page.
context
Values passed to page for custom page-specific logic.
Raises
PageValueError
When the component is not set for a non-404 page.
RouteValueError
When the specified route name already exists.
ValueError
When the route syntax is invalid.
get_load_events
get_load_events(path: str) -> list[IndividualEventType[()]]Get the load events for a route.
Parameters
path
The route to get the load events for.
Returns
The load events for the route.
add_all_routes_endpoint
add_all_routes_endpoint()Add an endpoint to the app that returns all the routes.
modify_state
modify_state(
token: BaseStateToken | str,
background: bool = False,
previous_dirty_vars: Optional[dict[str, set[str]]] = None,
**context: Unpack[StateModificationContext],
) -> AsyncIterator[BaseState]Modify the state out of band.
Parameters
token
The token to modify the state for.
background
Whether the modification is happening in a background task.
previous_dirty_vars
Vars that are considered dirty from a previous operation.
Yields
The state to modify.
Raises
RuntimeError
If the app has not been initialized yet.