3. Build your own Sphinx documentation

In this section you will:

  • create a brand new documentation set using rST/Sphinx

  • turn it into a Git repository

  • publish it on Read The Docs

  • explore some more advanced Sphinx features

You’ll need to:

  • be comfortable using the commandline

  • be familiar with the basics of Git

  • have Python 3 on your machine

Did you skip parts 1 and 2?

If so, before you continue, please add your name to the register as described in part 2, by making a pull request or using the GitHub interface.

Install Sphinx

Create and activate a virtual environment for Sphinx, for example:

python3 -m venv sphinxenv
source sphinxenv/bin/activate

(or however you prefer to do it).

Then install Sphinx:

pip install sphinx

Create the documentation set

In a new directory, run:

sphinx-quickstart

You’ll be asked some questions; accept the defaults where they’re offered, but provide them where required (the project name and your name).

Build the documentation

Just to prove that it has all worked:

make html

And if you accepted the defaults earlier:

open _build/html/index.html

to open it in your browser.

Add your own content

The index.rst file is the default home page of the documentation. Replace the entire contents of that file with something of your own, for example:

=================
All about my work
=================

Some basic formatting
=====================

.. sidebar:: This is a sidebar

    This is the body of the sidebar.

*Emphasis* usually appears in italics. **This strongly-emphasised text** will usually appear
in bold.

* item
* item


Some links
==========

The list of links below appears in a ``seealso`` directive.

.. seealso::

    * `Example <https://example.com>`_
    * `Python <https://python.org>`_


Code
====

Sphinx has a number of parsers for automated language highlighting in
code-blocks. It does a good job of guessing what language you're using,
but you can also state that explicitly as in this example:

 .. code-block:: bash
    :emphasize-lines: 2-3

    python3 -m venv sphinxenv
    source sphinxenv/bin/activate
    pip install sphinx
    pip freeze > requirements.txt
    sphinx-quickstart

And then rebuild with make html, checking that there are no errors, and that the HTML output is what you expected.

Add more pages

Add a couple of new pages alongside the index.rst, with titles and content of their own. For example, add:

successful-projects.rst
unsuccessful-projects.rst

Add a table of contents

In order for Sphinx to make the new pages available, it has to know what to do with them. The way to do this is to add a table of contents directive to index.rst:

.. toctree::
   :maxdepth: 1

   successful-projects
   unsuccessful-projects

The table of contents will appear wherever you put it in the home page - and also in the side navigation bar.

(This is just a very basic example. More complex nested structures are also possible, and you can arrange pages in folders for convenience.)

Run make html to check your changes and the output.

Apply a Sphinx theme

So far, your documentation is using the default Alabaster Sphinx theme. Try the Read the Docs theme, or Furo.

pip install sphinx-rtd-theme

Then in conf.py change the html_theme setting:

html_theme = "sphinx_rtd_theme"
../_images/rtd-theme.png
pip install furo

Then in conf.py change the html_theme setting:

html_theme = "furo"
../_images/furo-theme.png