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 Router

Class used for MkNav decorator routing.

mknodes.navs.navrouter.NavRouter

class NavRouter

Class used for MkNav decorator routing.

__call__

def __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__

def __init__(nav: mknav.MkNav) -> None

Constructor.

Parameters:

Name

Type

Description

Default

nav

mknav.MkNav

MkNav to use for routing

page

def 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..
168@nav.route.page(title, hide="toc")
169def _(page: mk.MkPage) -> None:
170    page += INTROS[title]
171    page += mk.MkCode(EXAMPLES[title].rstrip(), language="py", title="Example")
172    for identifier in identifiers:
173        page += mk.MkDocStrings(identifier, **DOCSTRING_OPTIONS)
Resources
Resources(css=[],
          markdown_extensions={},
          plugins=[],
          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