Routing
Instead of adding pages imperatively, functions can get registered for a nav path.
nav.route is a NavRouter bound to the nav,
Router collects routes without a nav (for example in a module which
gets imported by several build functions) and attaches them via register_nodes(nav).
The MkNavs / Using decorators section is built this way.
import mknodes as mk
nav = mk.MkNav("Guides")
@nav.route.page("First steps", icon="rocket")
def _(page: mk.MkPage):
page += "Some text"
@nav.route.nav("Advanced")
def _(nav: mk.MkNav):
nav.add_page("Plugins")
mknodes.Router
¶
Class used for MkNav decorator routing.
route
¶
Decorator method to use for routing.
The decorated functions need to return either a MkPage or an MkNav. They will get registered to the router-MkNav then.
In comparison to route_page() and route_nav(), it is not required
to know in advance what will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The section path for the returned |
()
|
route_nav
¶
route_nav(
*path: str, condition: Callable[..., Any] | bool | None = None, **kwargs: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator method to use for routing Navs.
The decorated functions will get passed an MkNav as an argument which can be modified then. They will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkNav and return it in the decorated method, that MkNav takes preference then over the modified one.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The section path for the returned MkNav |
()
|
condition
|
Callable[..., Any] | bool | None
|
If passed, the nav is only included if the callable returns True |
None
|
kwargs
|
Any
|
Keyword arguments passed to the MkNav constructor. |
{}
|
route_page
¶
route_page(
*path: str, condition: Callable[..., Any] | bool | None = None, **kwargs: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator method to use for routing Pages.
The decorated functions will get passed an MkPage as an argument which can be modified then. The resulting page will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkPage and return it in the decorated method, that MkPage takes preference then over the modified one.
The keyword arguments can be used to pass metadata-related keyword arguments to the MkPage constructor.
Examples:
Args: path: The section path for the returned MkPage condition: If passed, the page is only included if the callable returns True kwargs: Keyword arguments passed to the MkPage constructor.
mknodes.navs.navrouter.NavRouter
¶
Class used for MkNav decorator routing.
__call__
¶
Decorator method to use for routing.
The decorated functions need to return either a MkPage or an MkNav. They will get registered to the router-MkNav then.
In comparison to page() and nav(), it is not required to know in advance what
will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The section path for the returned |
()
|
nav
¶
Decorator method to use for routing Navs.
The decorated functions will get passed an MkNav as an argument which can be modified then. They will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkNav and return it in the decorated method, that MkNav takes preference then over the modified one.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The section path for the returned MkNav |
()
|
kwargs
|
Any
|
Keyword arguments passed to the MkNav constructor. |
{}
|
page
¶
Decorator method to use for routing Pages.
The decorated functions will get passed an MkPage as an argument which can be modified then. The resulting page will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkPage and return it in the decorated method, that MkPage takes preference then over the modified one.
The keyword arguments can be used to pass metadata-related keyword arguments to the MkPage constructor.
Examples:
Args: path: The section path for the returned MkPage kwargs: Keyword arguments passed to the MkPage constructor.
Page info
Code for this page
| mknodes.manual.api_section._add_page.<locals>._ | |
|---|---|