Sphinx is an open-source documentation generator primarily used for creating technical documentation for Python projects. It takes reStructuredText (reST) files, processes them, and generates various output formats, including HTML, PDF, LaTeX, ePub, and more.
-
Install Python 3 programming language, open your terminal, and create and activate the virtual environment in project folder:
cd path-to-your-project python -m venv .venv # For Linux, MacOS source .venv/bin/activate # For Windows with CMD .venv\Scripts\activate.bat # For Windows with Git Bash CLI source .venv/Scripts/activate
Note: To deactivate the virtual environment, simply run the following command in the terminal (on both Linux and Windows):
deactivate
-
Install and check Sphinx:
pip install sphinx sphinx-build --version
-
Set up the basic structure of the documentation project:
mkdir docs cd docs sphinx-quickstart # Set separate `source` and `build` directories and basic project parameters, like name, author(s), etc.
See the documentation structure:
tree . # On macOS, you need to install `brew install tree` first. . ├── Makefile # Building the documentation on Unix-like systems (Linux, macOS) ├── build # Output folder for built documentation ├── make.bat # Building the docs on Windows systems └── source ├── _static # Images, CSS files, or JavaScript files ├── _templates # Custom templates ├── conf.py # Configuration file for Sphinx settings └── index.rst # Root document, serves as the home page
-
Generate HTML doccumentation:
# Linux, MacOS make html # Windows make.bat html
Open
build/html/index.html
file in web browser and see the initial (empty) documentation page.Note: If you have LaTeX installed on your computer, you can generate documentation using the command:
make latexpdf
and find the pdf file inbuild/latex/
folder.
The default Sphinx theme is the Alabaster. If you want to change it, see the list of themes on Sphinx Themes Gallery, select your favorite one(s), such as Read the Docs, Furo, etc.
-
Download your favorite theme(s):
pip install sphinx-rtd-theme pip install furo
-
Change the default
alabaster
theme bysphinx_rtd_theme
orfuro
insource/conf.py
file... # html_theme = 'alabaster' html_theme = 'sphinx_rtd_theme' # html_theme = 'furo' ...
regenerate the documentation
# Linux, MacOS make html # Windows make.bat html
and reload the
index.html
page in web browser.
In the Sphinx documentation system, reStructuredText (lightweight markup language, often abbreviated as reST or .rst
) files are plain text files used to write the content of your documentation. Each .rst
file can represent a section or page of your documentation.
-
Create a new
installation.rst
file insource
folder and write its content, such as:Installation ============ To get started with MicroPython, follow these steps to install and run it on your hardware. 1. Install the `MicroPython <https://micropython.org/>`_ firmware on your board. 2. Install the `Thonny IDE <https://thonny.org/>`_. 3. Clone the course repository .. code-block:: console $ git clone https://github.com/tomas-fryza/esp-micropython.git 4. Run scripts from the :file:`examples` folder.
-
Update the main
source/index.rst
file and add the new documentation page:.. MicroPython Examples documentation master file, created by sphinx-quickstart on Sun Nov 10 14:04:56 2024. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. Welcome to MicroPython course's documentation! ============================================== General documentation --------------------- .. toctree:: :maxdepth: 2 installation
-
Regenerate the documentation and reload the
index.html
page in web browser.# Linux, MacOS make html # Windows make.bat html
Sphinx includes ways to automatically create the object definitions for your own code.
-
First, add the path to your Python modules and enable
sphinx.ext.autodoc
extension in Sphinx configuration filesources/conf.py
import sys import os sys.path.insert(0, os.path.abspath('../../modules')) # Here, the source files are located in `modules` folder: # . # ├── docs # │ ├── Makefile # │ ├── build # │ ├── make.bat # │ └── source # │ ├── conf.py # │ ├── index.rst # │ └── installation.rst # └── modules # └── wifi_module.py extensions = [ 'sphinx.ext.autodoc', # Core library for html generation from docstrings ]
-
Create a new documentation page
wifi.rst
insource
folder:Wi-Fi ===== .. automodule:: wifi_module :members: :undoc-members: :show-inheritance:
The
automodule
directive tells Sphinx to automatically extract documentation from thewifi_module.py
Python file frommodules
folder.:members:
includs all members (classes, functions, methods, etc.):undoc-members:
tells Sphinx to include also members that do not have docstrings:show-inheritance:
option is used to show the class inheritance hierarchy in the generated documentation
-
And finally, add the new documentation file to home page in
index.rst
... General documentation --------------------- .. toctree:: :maxdepth: 2 installation Modules ------- .. toctree:: :maxdepth: 1 wifi
-
Regenerate the documentation and reload the
index.html
page in web browser. This way you can add documentation to your other modules.# Linux, MacOS make html # Windows make.bat html
Some useful notes:
- You can list mock module(s), such as
machine
inconf.py
to avoid import errors in the build process:autodoc_mock_imports = [ 'machine', ]
- To add links to highlighted source code use the
sphinx.ext.viewcode
extension inconf.py
:extensions = [ 'sphinx.ext.autodoc', # Core library for html generation from docstrings 'sphinx.ext.viewcode', ]
See this exercise, how to use GitHub action to rebuild the documentation on git push
command.
Version | Result (yyyy-mm-dd) | Note |
---|---|---|
Windows 10 22H2 | OK (2024-11-21) | Lab SC 6.61 |
macOS Sonoma 14.6.1 | OK (2024-11-11) | MacBook |
FYI: How to identify the version of the operating system from the command line.
# Linux: cat /etc/os-release # or neofetch # macOS: sw_vers # Windows: WINVER