Six steps to mastering (automated) documentation for your python code
The importance of good documentation for our project is highly rated and regarded for a reason. Plain and simple, it enables others to understand, use and enhance our code.
But here’s another truth and let’s accept it, we as developers suck at it! And it’s okay because we are not authors or technical writers. We just like to build things and code!
So, how can we continue to do what we are good at as well as create something that is easily accepted by others (developers/users)?
Right! We are developers so we obviously have some automated tools at our doorstep. Let’s embrace one of them called sphinx (authored by Georg Brandl [respect!])
Six steps to mastering documentation for your python code
Step1: Python project structure
Step2: Code comments and README.md
Step3: Installing and Setting up
Step4: Generating the documentation
Step5: Uploading to github
Step6: Hosting documentation online
Step1: Python project structure
For the sake of clarity and more importantly not fighting the “module not found” error :), I will be using the following project structure:
[root]
--pythonic_documentation
--pythonic_documentation/docs/
--pythonic_documentation/resources/
--pythonic_documentation/src/
--pythonic_documentation/src/___init___.py
--pythonic_documentation/src/try_logging.py
--pythonic_documentation/src/...other files...
--pythonic_documentation/test/
--.....
In this structure please make note of the following:
1. docs/ is an empty folder at the beginning. It will contain:
1.1 Source files (.rst and .md files)
1.2 Build files (.html)
2. src/ contains our python code
resources/ may contain other supporting files like config files
Step2: Code comments and README.md
github has done a great job at making us all used-to having the bare minimum README.md. So, I wont delve into it.
For the api reference, the inline comments in our code are transformed into the api doc. I assume we should have some aspects of this covered in your code. However, if you are unsure, I highly recommend going through this 1 min read:
Step3: Installing and Setting up
We will be using the following two packages for automated document generation
- sphinx: ….is the heart of generating the documentation. By default, it reads the .rst files instead of .md
- recommonmark: ….is a utility that works with sphinx to help process .md files
Step3.1: Install the required packages:
> cd docs\
> pip install sphinx
> pip install recommonmark
Step3.2: Setup sphinx:
> sphinx-quickstart
## Use all defaults when asked for except for the following for which you should choose 'y'
autodoc: automatically insert docstrings from modules (y/n) [n]: y