Handlers
Handlers⚓︎
Handlers
- A handler is what makes it possible to collect and render documentation for a particular language.
Available Handlers⚓︎
Available Handlers
About the Python Handlers⚓︎
Python Handlers
Since version 0.18, a new Python handler is available. It is based on Griffe, which is an improved version of pytkdocs.
If you want to keep using the legacy handler as long as possible, you can depend on mkdocstrings-python-legacy directly, or specify the python-legacy extra when depending on mkdocstrings:
# PEP 621 dependencies declaration
# adapt to your dependencies manager
[project]
dependencies = [
"mkdocstrings[python-legacy]>=0.18",
]
The legacy handler will continue to "work" for many releases, as long as the new handler does not cover all previous use-cases.
Migrate to the New Python Handler⚓︎
New Python Handler
To use the new Python handler, you can depend on mkdocstrings-python directly, or specify the python extra when depending on mkdocstrings:
Selection Options⚓︎
NOTE
Since mkdocstrings 0.19, the YAML selection key is merged into the options key.
-
filtersis implemented, and used as before. -
membersis implemented, and used as before. -
inherited_membersis implemented. -
docstring_styleis implemented, and used as before, except for therestructured-textstyle which is renamedsphinx. Numpy-style is now built-in, so you can stop depending onpytkdocs[numpy-style]ordocstring_parser. -
docstring_optionsis implemented, and used as before.
Refer to the griffe documentation for the updated list of supported docstring options.
-
new_path_syntaxis irrelevant now. If you were setting it to True, remove the option and replace every colon (:) in your autodoc identifiers by dots (.).
Rendering Options⚓︎
NOTE
Since mkdocstrings 0.19, the YAML rendering key is merged into the options key.
Every previous option is supported. Additional options are available:
-
separate_signature: Render the signature (or attribute value) in a code block below the heading, instead as inline code. Useful for long signatures. If Black is installed, the signature is formatted. Default:False. -
line_length: The maximum line length to use when formatting signatures. Default:60. -
show_submodules: Whether to render submodules of a module when iterating on children. Default:False. -
docstring_section_style: The style to use to render docstring sections such as attributes, parameters, etc. Available styles:"table"(default),"list"and"spacy".
The SpaCy style is a poor implementation of their table style. We are open to improvements through PRs!
Templates⚓︎
Templates
Templates are mostly the same as before, but the file layout has changed, as well as some file names. See the documentation about the Python handler templates.
Custom Handlers⚓︎
Custom Handlers
Since version 0.14, you can create and use custom handlers thanks to namespace packages. For more information about namespace packages, see their documentation.
Tip
TIP: TL;DR - Project template for handlers.
-
mkdocstrings provides a Copier template to kickstart new handlers:handler-template.
-
To use it, install Copier (
pipx install copier), then runcopier gh:mkdocstrings/handler-template my_handlerto generate a new project. See its upstream documentation to learn how to work on the generated project.
Packaging⚓︎
Note!
- For mkdocstrings, a custom handler package would have the following structure:
📁 your_repository
└─ 📁 mkdocstrings_handlers
└─ 📁 custom_handler
├─ 📁 templates
│ ├─ 📁 material
│ ├─ 📁 mkdocs
│ └─ 📁 readthedocs
└─ 📄 __init__.py
Note The Absence of Module!
Note the absence of __init__.py module inmkdocstrings_handlers!
Code⚓︎
Code
-
A handler is a subclass of the base handler provided by mkdocstrings.
-
See the documentation for the
BaseHandler.
Code Subclasses
Subclasses of the base handler must declare a name and domain as class attributes, as well as implement the following methods:
collect(identifier, options)(required): method responsible for collecting and returning data (extracting documentation from source code, loading introspecting objects in memory, other sources? etc.)render(identifier, options)(required): method responsible for actually rendering the data to HTML, using the Jinja templates provided by your package.get_options(local_options)(required): method responsible for combining global options with local ones.get_aliases(identifier)(recommended): method responsible for returning known aliases of object identifiers, in order to register cross-references in the autorefs plugin.get_inventory_urls()(optional): method responsible for returning a list of URLs to download (object inventories) along with configuration options (for loading the inventory withload_inventory).load_inventory(in_file, url, **options)(optional): method responsible for loading an inventory (binary file-handle) and yielding tuples of identifiers and URLs.update_env(config)(optional): Gives you a chance to customize the Jinja environment used to render templates, for examples by adding/removing Jinja filters and global context variables.teardown()(optional): Clean up / teardown anything that needs it at the end of the build.
You must implement a get_handler method at the module level, which returns an instance of your handler. This function takes the following parameters:
theme(string, theme name)custom_templates(optional string, path to custom templates directory)mdx(list, Markdown extensions)mdx_config(dict, extensions configuration)handler_config(dict, handle configuration)tool_config(dict, the whole MkDocs configuration)
These arguments are all passed as keyword arguments, so you can ignore them by adding **kwargs or similar to your signature.
Do Not Modify the MkDocs Config!
- You should not modify the MkDocs config but can use it to get information about the MkDocs instance such as where the current
site_dirlives.
- See the Mkdocs Configuration for more info about what is accessible from it.
- Check out how the Python handler is written for inspiration.
Templates⚓︎
Templates
- Your handler's implementation should normally be backed by templates, which go to the directory
mkdocstrings_handlers/custom_handler/templates/some_theme(custom_handlerhere should be replaced with the actual name of your handler, andsome_themeshould be the name of an actual MkDocs theme that you support, e.g.material).
- With that structure, you can use
self.env.get_template("foo.html")inside yourrendermethod. This already chooses the subdirectory based on the current MkDocs theme.
- If you wish to support any MkDocs theme, rather than a few specifically selected ones, you can pick one theme's subdirectory to be the fallback for when an unknown theme is encountered. Then you just need to set the
fallback_themevariable on your handler subclass. The fallback directory can be used even for themes you explicitly support: you can omit some template from one of the other theme directories in case they're exactly the same as in the fallback theme.
- If your theme's HTML requires CSS to go along with it, you can put it into a file named, style.css, found at:
mkdocstrings_handlers/custom_handler/templates/some_theme/style.css, then this will be included into the final site automatically if this handler is ever used. Alternatively, you can put the CSS as a string into theextra_cssvariable of your handler.
Tell mkdocstrings to use the templates of another handler.
Finally, it's possible to entirely omit templates, and tell mkdocstrings to use the templates of another handler. In you handler, override the get_templates_dir() method to return the other handlers templates path:
Usage⚓︎
Usage
When a custom handler is installed, it is then available to mkdocstrings. You can configure it as usual:
plugins:
- mkdocstrings:
handlers:
custom_handler:
handler_config_option: yes
options:
some_config_option: "a"
other_config_option: 0
...and use it in your autodoc instructions:
Handler Extensions⚓︎
Handler Extensions
mkdocstrings provides a way for third-party packages to extend or alter the behavior of handlers. For example, an extension of the Python handler could add specific support for another Python library.
NOTE!
This feature is intended for developers. If you are a user and want to customize how objects are rendered, see Theming / Customization.
Such extensions can register additional template folders that will be used when rendering collected data. Extensions are responsible for synchronizing with the handler itself so that it uses the additional templates.
Defining an entry-point
An extension is a Python package that defines an entry-point for a specific handler:
[project.entry-points."mkdocstrings.python.templates"] # (1)!
extension-name = "extension_package:get_templates_path" # (2)!
- Replace
pythonby the name of the handler you want to add
templates to. - Replace
extension-nameby any name you want, and
and replaceextension_package:get_templates_pathby
the actual module path and function name in your package.
entry-point
This entry-point assumes that the extension provides a get_templates_path function directly under the extension_package package:
Function
This function doesn't accept any argument and returns the path (pathlib.Path or str) to a directory containing templates. The directory must contain one subfolder for each supported theme, even if empty (see "fallback theme" in custom handlers templates).
For example:
pyproject.toml
📁 extension_package/
├── __init__.py
└── 📁 templates/
├── 📁 material/
├── 📁 readthedocs/
└── 📁 mkdocs/
-
mkdocstrings will add the folders corresponding to the user-selected theme, and to the handler's defined fallback theme, as usual.
-
The names of the extension templates must not overlap with the handler's original templates.
-
The extension is then responsible, in collaboration with its target handler, for mutating the collected data in order to instruct the handler to use one of the extension template when rendering particular objects. See each handler's docs to see if they support extensions, and how.