Using decorators

Code for this section

routing.py
 1"""MkNodes routing example.
 2
 3MkNodes also supports setting up Navs via decorators.
 4"""
 5
 6import mknodes as mk
 7
 8
 9NAV_TEXT = """You can also use decorators to attach MkNavs. These navs then can continue
10to build the tree without using decorators (by adding sub-navs).
11"""
12
13nav = mk.MkNav("Using decorators")
14
15
16@nav.route.page("Routed page")
17def routed_page(page: mk.MkPage) -> None:
18    """Builds a MkPage and attaches it to the router MkNav."""
19    page += mk.MkAdmonition("I'm a page added via decorators!")
20
21
22@nav.route.page("Routed", "Deeply", "Nested", "Nested page")
23def routed_nested_page(page: mk.MkPage) -> None:
24    """Builds a nested MkPage and attaches it to the router MkNav."""
25    page += mk.MkAdmonition("I'm a nested page added via decorators!")
26
27
28@nav.route.nav("Routed", "Deeply", "Nested", "Nav")
29def routed_nav(nav: mk.MkNav) -> None:
30    """Builds a nested MkNav and attaches it to the router MkNav."""
31    index_page = nav.add_page(is_index=True)
32    index_page += mk.MkAdmonition(NAV_TEXT)
33    page = nav.add_page("Routed section page")
34    page += mk.MkAdmonition("Routed section page content")

MkNav.route Docstrings

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

nav

def 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

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 section
mknodes.manual.navs_section._
43@nav.route.nav("Routing")
44def _(nav: mk.MkNav) -> mk.MkNav:
45    page = routing.nav.add_page(is_index=True, icon="material/call-split", hide="toc")
46    page += mk.MkCode.for_file(routing.__file__, header="Code for this section")
47    page += mk.MkDocStrings(navrouter.NavRouter, header="MkNav.route Docstrings")
48    return routing.nav
Resources
Resources(css=[],
          markdown_extensions={},
          plugins=[],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/navs_section.py
  source_function: _
  source_line_no: 43
hide:
- toc
icon: material/call-split
title: Using decorators