Router
wybthon.router¶
router
¶
Client-side router components and navigation helpers for Pyodide apps.
This module exposes the browser-facing router built on top of
router_core:
Route: declarative mapping of a path pattern to a component, optionally with nested children.Router: function component that renders the matched route's component and provides theBasePathcontext.Link: anchor element that navigates via the History API and toggles an active class.navigate: programmatic navigation helper.current_path: aSignalcontaining the current pathname plus query string.
See Also
Classes:
| Name | Description |
|---|---|
Route |
Declarative route definition mapping a path to a component. |
Functions:
| Name | Description |
|---|---|
navigate |
Programmatically change the current path and update |
Router |
Function component that renders the matched route's component. |
Link |
Anchor element component that navigates via the History API. |
Attributes:
| Name | Type | Description |
|---|---|---|
current_path |
Signal[str]
|
Signal containing the current pathname plus query string. |
current_path
module-attribute
¶
Signal containing the current pathname plus query string.
Updated by navigate and by the global popstate
listener (back/forward navigation). Read it inside reactive scopes to
re-render when the URL changes.
BasePath
module-attribute
¶
BasePath = create_context('')
Route
dataclass
¶
Route(path: str, component: Union[Callable[[Dict[str, Any]], VNode], type], children: Optional[List['Route']] = None)
Declarative route definition mapping a path to a component.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Route pattern (e.g. |
component |
Union[Callable[[Dict[str, Any]], VNode], type]
|
A function component or class to render when the path matches. |
children |
Optional[List['Route']]
|
Optional nested routes whose paths are joined with
this route's |
navigate
¶
Router
¶
Function component that renders the matched route's component.
The render function is wrapped in a reactive hole so that updates
to current_path automatically
re-evaluate the matched route; no parent re-render is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
props
|
Any
|
The component's props with the following keys:
|
required |
Returns:
| Type | Description |
|---|---|
Any
|
A reactive |
Any
|
component, wrapped in a |
Any
|
publishes the active |
Any
|
Link
¶
Anchor element component that navigates via the History API.
Wrapped in a reactive hole so the active class flips automatically when the route changes; no parent re-render is required. Modifier-key clicks (Cmd/Ctrl/Shift) and middle-clicks are passed through to the browser so users can open links in a new tab.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
props
|
Any
|
The component's props with the following keys:
|
required |
Returns:
| Type | Description |
|---|---|
Any
|
A reactive |
API¶
class Route(path: str, component, children: Optional[List[Route]] = None)class Router(routes: List[Route], base_path: str = "", not_found: Optional[Callable[[Dict], VNode]] = None)function Link({to: str, children, base_path?: str, replace?: bool, class_active?: str})function navigate(path: str, replace: bool = False)signal current_path– reactive signal with currentpath + search
Notes:
- Wildcard path segment
*captures the rest of the path intoparams["wildcard"]. - Trailing
/*also matches the base segment; e.g./docs/*matches/docsand/docs/guide. - Nested routes use
childrenand resolve to the most specific match. Linkautomatically adds an active CSS class (default:"active") when itshrefmatches the current pathname. Customize withclass_active. Usereplace=Trueto avoid pushing a new history entry.