Skip to content

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.

Example
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

route(*path: str) -> Callable[[Callable[..., Any]], Callable[..., Any]]

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 MkNav / MkPage

()

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:

@router.route_nav("Routed nav")
def _(nav: mk.MkNav):
    nav += ...

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:

@router.route_page("Routed page", icon=...)
def _(page: mk.MkPage):
    page += ...

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__

__call__(*path: str) -> Callable[[AnyNavSubTypeCallable], Any]

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 MkNav / MkPage

()

__init__

__init__(nav: MkNav) -> None

Constructor.

Parameters:

Name Type Description Default
nav MkNav

MkNav to use for routing

required

nav

nav(*path: str, **kwargs: Any) -> Callable[[AnyNavCallable], 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:

@nav.route.nav("Routed nav")
def _(nav: mk.MkNav):
    nav += ...

Parameters:

Name Type Description Default
path str

The section path for the returned MkNav

()
kwargs Any

Keyword arguments passed to the MkNav constructor.

{}

page

page(*path: str, **kwargs: Any) -> Callable[[AnyPageCallable], 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:

@nav.route.page("Routed page", icon=...)
def _(page: mk.MkPage):
    page += ...

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>._
@nav.route.page(title, hide="toc")
def _(page: mk.MkPage) -> None:
    page += INTROS[title]
    page += mk.MkCode(EXAMPLES[title].rstrip(), language="py", title="Example")
    for identifier in identifiers:
        page += mk.MkDocStrings(identifier, **DOCSTRING_OPTIONS)
Resources
Resources(css=[],
          markdown_extensions={'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/api_section.py
  source_function: _add_page.<locals>._
  source_line_no: 168
hide:
- toc
title: Routing