Skip to content

Theming

A build function receives the theme next to the root nav. Theme holds the template overrides, CSS and the error page, MaterialTheme adds Material for MkDocs settings (palette, colors, content width, status icons).

Example
import mknodes as mk


def build(root: mk.MkNav, theme: mk.Theme) -> mk.MkNav:
    theme.error_page.content = mk.MkAdmonition("Page does not exist!")
    if isinstance(theme, mk.MaterialTheme):
        theme.content_area_width = 1300
    page = root.add_page("Home", is_index=True)
    page += "Hello"
    return root

mknodes.Theme

MkDocs Theme.

primary_color property

primary_color: str

Return the primary color of the theme.

Can be overridden by subclasses.

text_color property

text_color: str

Return the text color of the theme.

Can be overridden by subclasses.

__init__

__init__(name: str, *, data: dict[str, Any] | None = None) -> None

Instanciate the theme.

Parameters:

Name Type Description Default
name str

The theme name

required
data dict[str, Any] | None

Additional data for the theme

None

adapt_extensions

adapt_extensions(extensions: MutableMapping[str, dict[str, Any]]) -> None

Make adaptions to markdown extensions for the theme if required.

Parameters:

Name Type Description Default
extensions MutableMapping[str, dict[str, Any]]

The extensions to adapt

required

adapt_extras

adapt_extras(extras: dict[str, Any]) -> None

Adapt the "extras" dictionary containing additional information.

add_admonition_type

add_admonition_type(
    name: str,
    icon: str,
    header_color: ColorType,
    icon_color: ColorType | None = None,
    border_color: ColorType | None = None,
    font_color: ColorType | None = None,
) -> None

Add a custom admonition type.

Parameters:

Name Type Description Default
name str

Slug for new admonition type

required
icon str

Either a Pyconify icon key or an element

required
header_color ColorType

Color for the admonition header

required
icon_color ColorType | None

Color for the admonition icon

None
border_color ColorType | None

Color for the admonition border

None
font_color ColorType | None

Color for the admonition header font

None

get_css_context

get_css_context() -> dict[str, Any]

Return variables used to resolve the CSS template.

Can be overridden by subclasses.

get_resources async

get_resources() -> Resources

Return resources required for the theme.

Usually, the resources consist of static templates and CSS.

get_theme classmethod

get_theme(
    theme_name: str = "material", data: dict[str, Any] | None = None, **kwargs: Any
)

Return theme instance of corresponding subclass based on theme name.

Parameters:

Name Type Description Default
theme_name str

Name of the theme

'material'
data dict[str, Any] | None

Additional data for the theme

None
kwargs Any

Additional keyword arguments passe to the Theme

{}

mknodes.MaterialTheme

Bases: Theme

Material Theme.

alternate_selector instance-attribute

alternate_selector: list[dict[str, str]] = []

Layout: - name: English link: /en/ lang: en

palettes cached property

palettes: list[Palette]

Return a list of palettes used by the theme.

primary_color property

primary_color: str

Get the primary foreground color.

adapt_extensions

adapt_extensions(extensions: MutableMapping[str, dict]) -> None

MkDocs-Material needs some custom configuration for extensions.

This method will get called during the build process in order to make sure that theme-specifics are considered.

Parameters:

Name Type Description Default
extensions MutableMapping[str, dict]

Extensions to adapt.

required

add_status_icon

add_status_icon(name: str, icon: str, description: str | None = None) -> None

Add a custom status icon.

Parameters:

Name Type Description Default
name str

slug for the status icon

required
icon str

Iconify icon name

required
description str | None

Optional status description (used for tooltip)

None

add_tag

add_tag(identifier: str, tag: str, icon: str | None = None) -> None

Add a page tag.

The tag identifier must be set in page metadata. This requires the "tags" plugin to be included.

Parameters:

Name Type Description Default
identifier str

Slug for the tag

required
tag str

Tag name

required
icon str | None

Optional Iconify icon identifier

None

get_accent_color

get_accent_color() -> str

Get the accent foreground color.

get_css_context

get_css_context() -> dict[str, Any]

Return template context (used to render the CSS template.

set_accent_foreground_color

set_accent_foreground_color(color: RGBColorType)

Set the accent foreground color.

Parameters:

Name Type Description Default
color RGBColorType

Color to set

required

set_default_icon

set_default_icon(icon_type: IconTypeStr, data: str) -> None

Allows setting some custom default icons used throughout the theme.

Parameters:

Name Type Description Default
icon_type IconTypeStr

Icon type to set a new default for

required
data str

svg for the new icon

required

set_primary_background_color

set_primary_background_color(color: RGBColorType) -> str

Set the primary background color.

Parameters:

Name Type Description Default
color RGBColorType

Color to set

required

set_primary_foreground_color

set_primary_foreground_color(
    color: ColorType,
    light_shade: ColorType | None = None,
    dark_shade: ColorType | None = None,
)

Set a custom color theme.

Requires primary color set to "custom".

Parameters:

Name Type Description Default
color ColorType

Main color

required
light_shade ColorType | None

Optional light shade. If None, same as color.

None
dark_shade ColorType | None

Optional dark shade. If None, same as color.

None
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: Theming