Automatic documentation from docstrings, for mkdocs.
Project description
mkdocstrings
Automatic documentation from docstrings, for mkdocs.
This plugin is still in alpha status. Here is how it looks with the mkdocs-material theme for now:
Features
- Works great with Material theme:
mkdocstrings
was designed to work best with the great Material theme. - Support for type annotations:
mkdocstrings
uses your type annotations to display parameters types or return types. - Recursive documentation of Python objects: just write the module dotted-path, and you get the full module docs. No need to ask for each class, function, etc.
- Support for documented attribute: attributes (variables) followed by a docstring (triple-quoted string) will
be recognized by
mkdocstrings
, in modules, classes and even in__init__
methods. - Support for objects properties:
mkdocstrings
will know if a method is astaticmethod
, aclassmethod
or else, it will also know if a property is read-only or writable, and more! These properties will be displayed next to the object signature. - Every object has a TOC entry and a unique permalink: the navigation is greatly improved! Click the anchor next to the object signature to get its permalink, which is its Python dotted-path.
- Auto-reference other objects:
mkdocstrings
makes it possible to reference other Python objects from your markdown files, and even from your docstrings, with the classic Markdown syntax:[this object][package.module.object]
or directly with[package.module.object][]
. - Google-style sections support in docstrings:
mkdocstrings
understandsArguments:
,Raises:
andReturns:
sections. It will even keep the section order in the generated docs. - Support for source code display:
mkdocstrings
can add a collapsible div containing the source code of the Python object, directly below its signature, with the right line numbers. - Admonition support in docstrings: blocks like
Note:
orWarning:
will be transformed to their admonition equivalent. We do not support nested admonitions in docstrings! - Sane defaults: you should be able to just drop the plugin in your configuration and enjoy your auto-generated docs.
To get an example of what is possible, check mkdocstrings
'
own documentation, generated with itself.
Roadmap
- December 2019: It all started with a Proof of Concept. I familiarized myself with mkdocs and its plugin system, wrote quick and dirty code to retrieve Python objects with their docstrings and generate markdown from it. Markdown was automatically rendered to HTML in the mkdocs rendering process. It worked well but was really messy and not flexible at all.
- January 2020: I started designing a better architecture (mkdocs plugin + markdown extension), see issue #28
- January 2020: I still packaged it (with Poetry :heart:) and released the first versions to try it on real projects.
- January 2020: Some people tried it as well, contributed (thanks a lot!) and then someone with a lot of followers starred it and it got more attention.
- January 2020: It was the end of the year and the beginning of a new one, and I started a new job so my time was very limited. There were a few bug fixes, but overall slow progress.
- February 2020: Efforts are being put into the implementation of the desired architecture. Once it's done, it will be easier to contribute and things will move faster. It's just a bit hard right now because of my limited time, and the task requiring more than a few consecutive minutes to be done (more like a few consecutive hours).
- Mars 2020: At this point I'll make a new release (still 0.x of course)
- Mars 2020: And obviously we'll need an extensive test suite to ensure quality :slightly_smiling_face:
- Mars-April 2020: We'll make mkdocstrings configurable (for selecting objects and rendering them)
- April 2020: Work the backlog!
Requirements
mkdocstrings requires Python 3.6 or above.
To install Python 3.6, I recommend using pyenv
.
# install pyenv
git clone https://github.com/pyenv/pyenv ~/.pyenv
# setup pyenv (you should also put these three lines in .bashrc or similar)
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"
# install Python 3.6
pyenv install 3.6.8
# make it available globally
pyenv global system 3.6.8
Installation
With pip
:
python3.6 -m pip install mkdocstrings
Usage
# mkdocs.yml
# designed to work best with material theme
theme:
name: "material"
plugins:
- search
- mkdocstrings
In one of your markdown files:
# Reference
::: my_library.my_module.my_class
You can reference objects from other modules in your docstrings:
def some_function():
"""
This is my function.
It references [another function][package.submodule.function].
It also references another object directly: [package.submodule.SuperClass][].
"""
pass
Add some style in docs/custom.css
:
div.autodoc {
padding-left: 25px;
border-left: 4px solid rgba(230, 230, 230);
}
And add it to your configuration:
extra_css:
- custom.css
Docstrings format
Your docstrings must follow a particular format, otherwise mkdocstrings
will throw an exception.
This will be improved to be more robust over time.
from typing import Optional
def my_function(param1: int, param2: Optional[str] = None) -> str:
"""
A short description of this function.
A longer description of this function.
You can use more lines.
This is code block,
as usual.
```python
s = "This is a Python code block :)"
```
Arguments:
param1: An integer?
param2: A string? If you have a long description,
you can split it on multiple lines.
Just remember to indent those lines with at least two more spaces.
They will all be concatenated in one line, so do not try to
use complex markup here.
Note:
We omitted the type hints next to the parameters names.
Usually you would write something like `param1 (int): ...`,
but `mkdocstrings` gets the type information from the signature, so it's not needed here.
Exceptions are written the same.
Raises:
OSError: Explain when this error is thrown.
RuntimeError: Explain as well.
Multi-line description, etc.
Let's see the return value section now.
Returns:
A description of the value that is returned.
Again multiple lines are allowed. They will also be concatenated to one line,
so do not use complex markup here.
Note:
Other words are supported:
- `Args`, `Arguments`, `Params` and `Parameters` for the parameters.
- `Raise`, `Raises`, `Except`, and `Exceptions` for exceptions.
- `Return` or `Returns` for return value.
They are all case-insensitive, so you can write `RETURNS:` or `params:`.
"""
return f"{param2}{param1}"
This docstring would be rendered like this (had to take two screenshots, so it's not perfectly aligned):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mkdocstrings-0.7.1.tar.gz
.
File metadata
- Download URL: mkdocstrings-0.7.1.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.6.9 Linux/5.4.3-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bb046efa9f450aa03b2ec65aa65fc9e44a027f75b3f5e4b2fcc252387c3afaf |
|
MD5 | 6d3df919d3a8127a39811c46098f98c8 |
|
BLAKE2b-256 | 65b250458fa55a0e938364eae9d632f770021ed7277451793c25d6d5b330e8bd |
Provenance
File details
Details for the file mkdocstrings-0.7.1-py3-none-any.whl
.
File metadata
- Download URL: mkdocstrings-0.7.1-py3-none-any.whl
- Upload date:
- Size: 23.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.6.9 Linux/5.4.3-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb71b4d1e0f1e5b29ce30a71fb48ea86bdf83d7c26991b9060adbec5dd45677a |
|
MD5 | eeeeb7015c4468a56c408086d0194be6 |
|
BLAKE2b-256 | 213b310f55302189e7d9b838c844c545de9e59957fb153c62ca00a36eb9f874f |