2021-05-25 03:56:22 -05:00
|
|
|
|
.. _tutorial:
|
|
|
|
|
|
|
|
|
|
===============
|
|
|
|
|
Sphinx tutorial
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
In this tutorial you will build a simple documentation project using Sphinx,
|
2021-05-26 13:58:57 -05:00
|
|
|
|
and view it in your browser as HTML.
|
|
|
|
|
The project will include narrative, handwritten documentation,
|
2021-05-25 03:56:22 -05:00
|
|
|
|
as well as autogenerated API documentation.
|
|
|
|
|
|
2021-05-26 13:58:57 -05:00
|
|
|
|
The tutorial is aimed towards Sphinx newcomers
|
|
|
|
|
willing to learn the fundamentals of how projects are created and structured.
|
|
|
|
|
You will create a fictional software library to generate random food recipes
|
|
|
|
|
that will serve as a guide throughout the process,
|
|
|
|
|
with the objective of properly documenting it.
|
|
|
|
|
|
2021-05-29 06:44:26 -05:00
|
|
|
|
To showcase Sphinx capabilities for code documentation
|
|
|
|
|
you will use Python,
|
|
|
|
|
which also supports *automatic* documentation generation.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Several other languages are natively supported in Sphinx
|
|
|
|
|
for *manual* code documentation,
|
|
|
|
|
however they require extensions for *automatic* code documentation,
|
|
|
|
|
like `Breathe <https://breathe.readthedocs.io/>`_.
|
2021-05-25 03:56:22 -05:00
|
|
|
|
|
2021-05-26 13:58:57 -05:00
|
|
|
|
To follow the instructions you will need access to a Linux-like command line
|
|
|
|
|
and a basic understanding of how it works,
|
|
|
|
|
as well as a working Python installation for development,
|
2021-05-29 12:15:15 -05:00
|
|
|
|
since you will use *Python virtual environments* to create the project.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
Getting started
|
|
|
|
|
---------------
|
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
|
Setting up your project and development environment
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
|
In a new directory,
|
2021-05-26 13:58:57 -05:00
|
|
|
|
create a file called ``README.rst``
|
2021-05-31 08:48:57 -05:00
|
|
|
|
with the following content.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
.. code-block:: rest
|
|
|
|
|
|
|
|
|
|
Lumache
|
|
|
|
|
=======
|
|
|
|
|
|
|
|
|
|
**Lumache** (/luˈmake/) is a Python library for cooks and food lovers
|
|
|
|
|
that creates recipes mixing random ingredients.
|
|
|
|
|
|
|
|
|
|
It is a good moment to create a Python virtual environment
|
|
|
|
|
and install the required tools.
|
|
|
|
|
For that, open a command line terminal,
|
|
|
|
|
``cd`` into the directory you just created,
|
|
|
|
|
and run the following commands:
|
|
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
$ python -m venv .venv
|
|
|
|
|
$ source .venv/bin/activate
|
|
|
|
|
(.venv) $ python -m pip install sphinx
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
The installation method used above
|
|
|
|
|
is described in more detail in :ref:`install-pypi`.
|
|
|
|
|
For the rest of this tutorial,
|
|
|
|
|
the instructions will assume a Python virtual environment.
|
|
|
|
|
|
|
|
|
|
If you executed these instructions correctly,
|
|
|
|
|
you should have the Sphinx command line tools available.
|
|
|
|
|
You can do a basic verification running this command:
|
|
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
(.venv) $ sphinx-build --version
|
|
|
|
|
sphinx-build 4.0.2
|
|
|
|
|
|
|
|
|
|
If you see a similar output, you are on the right path!
|
|
|
|
|
|
|
|
|
|
Creating the documentation layout
|
2021-05-29 06:21:47 -05:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
|
Then from the command line,
|
2021-05-26 13:58:57 -05:00
|
|
|
|
run the following command:
|
|
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
(.venv) $ sphinx-quickstart docs
|
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
|
This will present to you a series of questions
|
2021-05-26 13:58:57 -05:00
|
|
|
|
required to create the basic directory and configuration layout for your project
|
2021-05-31 08:48:57 -05:00
|
|
|
|
inside the ``docs`` folder.
|
2021-05-31 09:35:35 -05:00
|
|
|
|
To proceed, answer each question as follows:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-06-06 03:56:23 -05:00
|
|
|
|
- ``> Separate source and build directories (y/n) [n]``: Write "``y``"
|
|
|
|
|
(without quotes) and press :kbd:`Enter`.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
- ``> Project name``: Write "``Lumache``" (without quotes)
|
|
|
|
|
and press :kbd:`Enter`.
|
|
|
|
|
- ``> Author name(s)``: Write "``Graziella``" (without quotes)
|
|
|
|
|
and press :kbd:`Enter`.
|
|
|
|
|
- ``> Project release []``: Write "``0.1``" (without quotes)
|
|
|
|
|
and press :kbd:`Enter`.
|
|
|
|
|
- ``> Project language [en]``: Leave it empty (the default, English)
|
|
|
|
|
and press :kbd:`Enter`.
|
|
|
|
|
|
|
|
|
|
After the last question,
|
2021-05-31 08:48:57 -05:00
|
|
|
|
you will see the new ``docs`` directory with the following content.
|
|
|
|
|
|
|
|
|
|
.. code-block:: text
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-31 09:29:09 -05:00
|
|
|
|
docs
|
2021-05-26 13:58:57 -05:00
|
|
|
|
├── build
|
|
|
|
|
├── make.bat
|
|
|
|
|
├── Makefile
|
|
|
|
|
└── source
|
|
|
|
|
├── conf.py
|
|
|
|
|
├── index.rst
|
|
|
|
|
├── _static
|
|
|
|
|
└── _templates
|
|
|
|
|
|
2021-05-31 09:35:35 -05:00
|
|
|
|
The purpose of each of these files is:
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-29 06:22:21 -05:00
|
|
|
|
``build/``
|
|
|
|
|
|
|
|
|
|
An empty directory (for now)
|
2021-05-26 13:58:57 -05:00
|
|
|
|
that will hold the rendered documentation.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
|
|
``make.bat`` and ``Makefile``
|
|
|
|
|
|
|
|
|
|
Convenience scripts
|
2021-05-26 13:58:57 -05:00
|
|
|
|
to simplify some common Sphinx operations,
|
|
|
|
|
such as rendering the content.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
|
|
``source/conf.py``
|
|
|
|
|
|
|
|
|
|
A Python script holding the configuration of the Sphinx project.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
It contains the project name and release you specified to ``sphinx-quickstart``,
|
|
|
|
|
as well as some extra configuration keys.
|
2021-05-29 06:22:21 -05:00
|
|
|
|
|
|
|
|
|
``source/index.rst``
|
|
|
|
|
|
|
|
|
|
The :term:`master document` of the project,
|
2021-05-26 13:58:57 -05:00
|
|
|
|
which serves as welcome page
|
|
|
|
|
and contains the root of the "table of contents tree" (or *toctree*).
|
|
|
|
|
|
|
|
|
|
Thanks to this bootstrapping step,
|
|
|
|
|
you already have everything needed
|
|
|
|
|
to render the documentation as HTML for the first time.
|
|
|
|
|
To do that, run this command:
|
|
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
|
.. code-block:: console
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
|
|
|
|
(.venv) $ sphinx-build -b html docs/source/ docs/build/html
|
|
|
|
|
|
|
|
|
|
And finally, open `docs/build/html/index.html` in your browser.
|
|
|
|
|
You should see something like this:
|
|
|
|
|
|
|
|
|
|
.. image:: /_static/tutorial/lumache-first-light.png
|
|
|
|
|
|
2021-05-27 11:17:33 -05:00
|
|
|
|
There we go! You created your first HTML documentation using Sphinx.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-27 14:32:49 -05:00
|
|
|
|
Making some tweaks to the index
|
2021-05-29 06:21:47 -05:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-27 14:32:49 -05:00
|
|
|
|
The ``index.rst`` file that ``sphinx-quickstart`` created
|
|
|
|
|
has some content already,
|
|
|
|
|
and it gets rendered as the front page of our HTML documentation.
|
|
|
|
|
It is written in reStructuredText,
|
|
|
|
|
a powerful markup language.
|
2021-05-26 13:58:57 -05:00
|
|
|
|
|
2021-05-31 09:35:35 -05:00
|
|
|
|
Modify the file as follows:
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
|
|
|
|
.. code-block:: rest
|
|
|
|
|
|
|
|
|
|
Welcome to Lumache's documentation!
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
|
|
**Lumache** (/luˈmake/) is a Python library for cooks and food lovers
|
|
|
|
|
that creates recipes mixing random ingredients.
|
|
|
|
|
It pulls data from the `Open Food Facts database <https://world.openfoodfacts.org/>`_
|
|
|
|
|
and offers a *simple* and *intuitive* API.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
This project is under active development.
|
|
|
|
|
|
|
|
|
|
This showcases several features of the reStructuredText syntax, including:
|
|
|
|
|
|
|
|
|
|
- a **section header** using ``===`` for the underline,
|
2021-05-31 09:22:37 -05:00
|
|
|
|
- two examples of :ref:`rst-inline-markup`: ``**strong emphasis**`` (typically bold)
|
2021-05-29 06:22:36 -05:00
|
|
|
|
and ``*emphasis*`` (typically italics),
|
2021-05-27 14:32:49 -05:00
|
|
|
|
- an **inline external link**,
|
2021-06-06 03:56:23 -05:00
|
|
|
|
- and a ``note`` **admonition** (one of the available
|
|
|
|
|
:ref:`directives <rst-directives>`)
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
2021-05-31 08:48:57 -05:00
|
|
|
|
Now to render it with the new content,
|
2021-05-27 14:32:49 -05:00
|
|
|
|
you can use the ``sphinx-build`` command as before,
|
2021-05-31 09:35:35 -05:00
|
|
|
|
or leverage the convenience script as follows:
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
2021-05-31 11:03:04 -05:00
|
|
|
|
.. code-block:: console
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
2021-05-31 09:29:55 -05:00
|
|
|
|
(.venv) $ cd docs
|
|
|
|
|
(.venv) $ make html
|
2021-05-27 14:32:49 -05:00
|
|
|
|
|
|
|
|
|
After running this command,
|
|
|
|
|
you will see that ``index.html`` reflects the new changes!
|
|
|
|
|
|
|
|
|
|
Where to go from here
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
This tutorial covered
|
|
|
|
|
the very first steps to create a documentation project with Sphinx.
|
|
|
|
|
To continue learning more about Sphinx,
|
|
|
|
|
check out the :ref:`rest of the documentation <contents>`.
|