Documenting Python Code With Sphinx
How to write Python code to automatically create documentation
Sphinx is a tool to create documentation for Python projects. It creates HTML documentation directly from docstrings.
We'll use the project of the CoinGecko API to show how to use Sphinx to create stunning documentation for Python projects.
Prerequisites
Before using Sphynx you have to create and activate a virtual environment.
This section shows how to do so in Windows and Linux. So, if you already know it you can skip it.
Creating a virtual environment on a Windows machine
Step 1: install virtualenv
> pip install virtualenvStep 2: create the virtual environment
Go to the directory where you want to create the virtual environment. Suppose, in that directory, you want to create a directory venv then type:
> python -m venv venvThis code creates a folder called venv which contains everything you need for your virtual environment.
Step 3: activate the virtual environment
Remain in the same folder you are in and type:
> venv/Scripts/activateCreating a virtual environment on a Linux machine
Step 1: verify you need any updates
$ sudo apt-get updateStep 2: install the virtual environment packages
$ sudo apt install python3-venvStep 3: create the virtual environment
Go to the directory where you want to create the virtual environment. Suppose, in that directory, you want to create a directory venv then type:
$ python3 -m venv venvThis code creates a folder called venv which contains everything you need for your virtual environment.
Step 4: activate the virtual environment
Remain in the same folder you are in and type:
$ source venv/bin/activateHow to use Sphinx
To use Sphinx to create documentation for your Python code, follow this procedure.
Step 1: activate a virtual environment and create a /docs folder
Open a CLI and, after activating the virtual environment, create a /docs folder in the main folder of your project.
Step 2: install Sphinx
Go to the /docs folder ( cd docs) and install Shinx as follows:
python -m pip install sphinxStep 3: install the theme you like
Install the theme you like by choosing one from here.
In this example, we install the "Piccolo theme":
pip install piccolo-themeStep 4: initialize Sphinx
Type the following to initialize Sphinx:
sphinx-quickstartYou’ll be asked some questions. Answer as follows:
separate source and build directories? → y
project name → (the name of your project)
Author name → (the name of the author)
project release → 0.0
project language → press ENTER to leave it as English (default)
Now the /docs folder has been populated with the files and subfolders by Sphinx.
Copy all the files inside the /source folder in the /docs folder and delete the folder /source. Here's how the /docs folder should look like after that:

Step 5: first Sphinx command
Via CLI, go to the main folder of your project and type the following:
sphinx-apidoc -o docs .Step 6: change the index.rst file
Inside /docs/source folder there is an index.rst file. You need this file to point to the modules.rst file.
So, open index.rst, for example via VS CODE, and write module as follows:

Step 7: change the conf.py file
Inside /docs/source folder there is an conf.py file.
This is what it looks like:

Modify it as follows:
Change
extensions = []inextensions = ["sphinx.ext.todo", "sphinx.ext.viewcode", "sphinx.ext.autodoc"].Change
html_theme = 'alabaster' by inserting the theme you choose. We've chosen the "Piccolo theme" so we change it tohtml_theme = 'piccolo_theme'.Tell the program, in the beginning of it, to use the absolute path like so:
import osimport syssys.path.insert(0, os.path.abspath(".."))
So, this is how the conf.py file looks like after the modifications:

Step 8: create the build
Verify you're in the main folder of the project and type the following:
sphinx-build -M html docs docs/build/htmlNow, inside docs\build\html\html you find a index.html file: open with your browse to visualize the code documentation.
This is how the documentation of your Python project looks like when you open it:

Of course, to reach this result, you must implement best practices like docstrings and type hints as PEP8 recommends.
WARNINGS:
1) I recommend using Sphinx when you're sure the project is frozen for quite some time. Otherwise, at each code change, you must re-build the project (in this case, you'll increment the review number described in step 4 from 0.0 to 1.0 or whatever the review number it should be for your case).
2) After installing Sphinx and the theme you've chosen for the first time, of course, you don't need to do so anymore. So, after the second time, you can start from step 4 of this guide.
Last updated