Sphinx Development Environment¶
We use a Python library called Sphinx to generate documentation from ReST files.
If you haven’t already installed Python 3, see Installing Python. When you come to the end of the Python installation section, come back to this guide.
Installing Sphinx on Linux¶
In Ubuntu, Sphinx is very easy to install. You can install it for either Python 2 or Python 3. Since we use the latter, we’ll install that.
To install on Ubuntu, simply run…
$ sudo apt install python3-sphinx pandoc
Installing Sphinx on Mac¶
We can install Sphinx on Mac using either Homebrew or MacPorts.
Installing Via MacPorts¶
$ sudo port install py36-sphinx pandoc $ sudo port select --set sphinx py36-sphinx
Installing Via Homebrew¶
$ brew install sphinx-doc pandoc
Installing Sphinx on Windows¶
You can install Sphinx using Python pip.
python -m pip install -U sphinx
Then, download and install Pandoc from its official website: http://www.pandoc.org/installing.html.
Choosing an Editor¶
ReST files are just plain text files written in the ReStructuredText markup language. Thus, you can edit them in any text editor.
If you’re using VSCode, make sure you install the Esbonio extension by Swyddfa.
Writing and Editing Documentation¶
To get started editing documentation, create or open a file in the
docs/source directory of the repository for whatever project you’re
documenting. (Some projects may put documentation in a different outer
source/index.rst is the main file in your documentation. To add a
file to the automatic table of contents tree, list it below this section:
.. toctree:: :maxdepth: 2
You only need to list the name of the file, without the extension. If it is
within a subfolder in
source/, just write out the relative path.
For example, if you had the file
source/bar/, you can add them like this:
.. toctree:: :maxdepth: 2 foo bar/baz
Note that I lined everything up, so both of my new entries have
the same number of leading spaces as
:maxdepth: 2. ReST is VERY
finicky! You should line things up exactly.
Adding a New File¶
To add a new file to your documentation, create a file in the
docs/source directory (or a subdirectory thereof) with the
A good filename should be all lowercase, with underscores where necessary. For
goldilocks.rst would be a good file name for the Goldilocks
Documentation pages can be quite long, and that’s fine. Sphinx subdivides large documents beautifully, so you can generally devote one single document to one single module, section, or topic.
Once you click Save, your editor will probably know to treat the file as a ReST document.
Rendering Final Output¶
It is super simple to render the gorgeous output of your documentation with
Sphinx. In your Terminal, go to your
docs/ folder, and simply type…
…to list out all the supported formats. My favorite is HTML, which generates a snazzy, searchable web version. To output to HTML, type…
$ make html
Double-check the Terminal output. If it complains about any errors or warnings, be sure to fix them! Errors are pretty obvious in Sphinx - missing documents, malformed tables, and so on.
Because Sphinx is the standard way of creating documentation for Python projects, the #python IRC channel is a really good place to get help.
Once the HTML is created, go to
build/html/, and open
in your favorite web browser.
Setting Up Documentation¶
If your project already has documentation, skip this section!
In the repository for the project that you want to create the documentation for, run…
$ mkdir docs $ cd docs
This creates a new folder
docs for our documentation, and navigates into
it. Then, run…
Below are the settings you should select during Quickstart. I’ve marked ENTER for those options that you should use the default on.
Root path: ENTER
Separate source and build:
Name prefix for templates and static dir:
Project name: <name of your project>
Author name(s): MousePaw Media
Project version: <short project version>
Project release: <long project version>
Project language: Enter
Source file suffix: Enter
Name of your master document: Enter
Do you want to use the epub builder?:
Use defaults for the rest of the options.
You can change most of those options again later.
Before continuing, you should also edit your
.gitignore file, adding
This ensures that Sphinx’s output is not tracked by the repository.
source/conf.py is the configuration file for Sphinx. This is where you
change things like project name, author, copyright, and version, as well as
build options and theme.
If you just created this documentation directory, open this file
and then look for
html_theme. Change this from
sphinx_rtd_theme. The new line should look like this:
html_theme = 'sphinx_rtd_theme'
ReStructuredText is a markup language all its own. It has a ton and a half of awesome features, but it’s hard to know them all. Check out Sphinx’s documentation for help with all things Sphinx and ResT! I’ve linked you above to the best page to start with.