mkdocstrings
mkdocstrings is an MkDocs plugin that generates automatic documentation from source code, providing an alternative to Sphinx. It leverages 'handlers' (like mkdocstrings-python) to parse different language sources and render them within your MkDocs site. The current version is 1.0.3, with frequent patch releases and occasional minor/major feature releases.
Warnings
- breaking Version 1.0.0 introduced significant breaking changes to the `BaseHandler` API. If you have custom handlers or extend `mkdocstrings` programmatically, your code will likely break.
- deprecated Importing public objects directly from submodules (e.g., `mkdocstrings.handlers.python.PythonHandler`) was deprecated in 0.28.3 and will raise errors in v1.x. Public objects should now be imported from the top-level `mkdocstrings` module.
- gotcha mkdocstrings requires a language-specific 'handler' to actually parse and document code (e.g., for Python, `mkdocstrings-python`). Installing `mkdocstrings` alone is often not enough.
- gotcha Incorrect or missing plugin configuration in `mkdocs.yml` is a common source of errors. Users often forget to list `mkdocstrings` under the `plugins:` section or misconfigure handler-specific options like `paths`.
- gotcha When using `mkdocstrings` with `mkdocs-autorefs`, the order of plugins in `mkdocs.yml` matters. `mkdocstrings` should generally be listed *before* `mkdocs-autorefs` to ensure that references are properly resolved.
Install
-
pip install "mkdocstrings[python]" -
pip install mkdocstrings
Imports
- MkdocstringsPlugin
from mkdocstrings.plugin import MkdocstringsPlugin
Quickstart
# mkdocs.yml
site_name: My Awesome Project
theme: material
plugins:
- mkdocstrings: # Enable the mkdocstrings plugin
handlers:
python:
paths: [src] # Tell the Python handler where to find your code
# src/my_module.py
def greet(name: str) -> str:
"""
Greets a person by their name.
Parameters:
name: The name of the person to greet.
Returns:
A personalized greeting string.
"""
return f"Hello, {name}!"
# docs/index.md
# Welcome
This is the main documentation page.
## API Reference
::: src.my_module.greet
options:
show_source: false