Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Installation¶
You can install Sphinx and necessary extensions using the following command.
:::bash
pip3 install sphinx sphinx-autodoc-typehintsOr simply
:::bash
xinstall sphinx -icSince the above commands installs Sphinx to the user’s local directory,
Sphinx executables are placed into the directory ~/.local/bin.
So you might have to configure your PATH environment variable
so that you can use Sphinx commands directly.
Generate Docs Using Sphinx¶
Create a directory named
docsin the root directory of your Python project. Do NOT use the root directory of your project as the root directory for documentations as it will make your the root directory of your project messy.:::bash mkdir docsGo to the directory
docsand run the commandsphinx-quickstart.:::bash cd docs sphinx-quickstartA few questions will be asked to you. Note: It is suggested that you choose to separate build and source directories when the question is asked. This option makes the
docsdirectory tidier especially for large projects.Update the generated configuration script
conf.py. Below are a few important ones.Configure
sys.pathto tell autodoc where to find your code. In short, you should insert the path to your source code directory as the first element tosys.path. Relative paths (w.r.t the directory of the fileconf.py) are allowed. Assume your project has the following structure,:::text proj_name/ proj_name/ __init__.py docs/ ...you can use the following generic code to help you to insert the correct path of the source code directory.
:::python import sys from pathlib import Path def get_source_dir() -> str: path = Path(__file__).resolve() while path.name != "docs": path = path.parent return str(path.parent) sys.path.insert(0, get_source_dir())Enable sphinx extensions.
:::python extensions = [ "sphinx.ext.todo", "sphinx.ext.viewcode", "sphinx.ext.autodoc", "sphinx_autodoc_typehints", "sphinx.ext.doctest", ]
Run the following command in the
docsdirectory to generate documentation from docstrings. Notice that the commandsphinx-apidocdoes not extract docstrings from your source code but instead generate a RST file to tell Sphinx to use docstrings when building the docs. So you only have to run the commandaphinx-apidoconce.:::bash # if build and source are NOT separated sphinx-apidoc -f -o ./ ../proj_name # if build and source are separated sphinx-apidoc -f -o source/ ../proj_nameA file named
modules.rst(together with some other RST files) will be generated. Include it into the fileindex.rst.Add more RST files into
index.rstif necessary.Run the following command in the
docsdirectory to generate HTML documentation.:::bash make clean && make html
Use Markdown Together with RST in Sphinx¶
Please refer to the official documentation markdown for instructions.
Public Host¶
Generate Python Documentation¶
https://
https://
AutoDoc¶
sphinx.ext.autodoc
https://
http://
https://
Sphinx Extensions¶
References¶
https://
https://
http://
https://
https://medium.com/richdayandnight/a-simple-tutorial-on-how-to-document-your-python-project-using-sphinx-and-rinohtype-177c22a15b5b