streamlitextras.router package
- class Router(routes: dict[str, Callable], preroute: Optional[Callable] = None, dependencies: Optional[dict] = None, postroute: Optional[Callable] = None, debug: bool = False)[source]
Bases:
objectPage router for streamlit.
- Parameters
routes (dict[str, Callable]) – Dictionary mapping of routes to their page functions, in the format {page_name: page_function}
preroute (Optional[Callable]) – Optional callable page function that will be executed before each page function
dependencies (Optional[list]) – Optional dict to pass as kwargs to every page_function call
postroute (Optional[Callable]) – Optional callable page function that will be executed after each page function
- delayed_init()[source]
Used to delay initialization of streamlit objects so this class can be cached
- property default_page
Returns the default page. Currently the first in self.routes
- property current_page
- property current_state
- property query_params
Uses streamlit to fetch the current URL query parameters, removing the current page param and state value.
- property query_params_all
Uses streamlit to fetch the current URL query parameters, including the current page param and state value.
- show_route_view(force_page_name: Optional[str] = None, args: Optional[tuple] = None, kwargs: Optional[dict] = None, redirect_page_names: Optional[list[str]] = None)[source]
Checks the query params and routes to the requested page, or routes to force_page_name directly, not setting any query params
- Parameters
force_page_name – If provided will route to this page via its function in self.routes, bypassing query string params.
args – Tuple of args to pass to the page func for force_page_name
kwargs – Dict of kwargs to pass to the page func for force_page_name They will take precedence over and be merged with self.dependencies
redirect_page_names – If page name from query string is in this list, will be redirected to the default route instead. Useful for conditional redirection in authentication etc.
- route(page_name: Optional[str] = None, page_state: Optional[str] = None, additional_params: Optional[dict] = None, rerun_st: bool = False)[source]
Routes to a page. First found query string matching a page key in self.routers is routed too. Query string value can be set to page data
- Parameters
page_name (Optional[str]) – The key for the page in self.routes - query param key will be set the same If it is None first page in self.routes will be used, and no query params will be set (redirect to /)
page_state (Optional[str]) – Optional string to include as page state, will be urlencoded/urldecoded
additional_params (Optional[dict]) – Optional dict to be set as query parameters using st.experimental_set_query_params If you use the page name as one of the keys, behaviour is overriding and may be experimental
rerun_st (bool) – Whether to call st.experimental_rerun() - not needed if calling this from a st callback