Sphinx est un générateur de documentation libre. Il a été développé par Georg Brandl pour la communauté Python en 2008, et est le générateur de la documentation officielle de projets tels que Python, Django, Selenium, Urwid, ou encore Bazaar.

–> C’est du lourd ! 🙂

Installation de Sphinx :

Vous vous rendez dans le répertoire de votre installation Python où se trouve l’exécutable python.exe. Pour moi, C:\Python34. Vous cliquez droit dans ce répertoire en maintenant la touche Majuscule enfoncée et vous choisissez dans le menu contextuel “Ouvrir une fenêtre de commande ici”.

Vous tapez pip install -U sphinx
Si vous voulez installer la version beta : pip install -U –pre sphinx

Pour avoir eu une mauvaise expérience d’installation sur un PC (modules non installés et lignes rouges dans CMD), j’ai recommencé celle-ci en ouvrant la console CMD en administrateur ! A ce moment-là, tout s’est bien passé ! 🙂 

C:\Python34>pip install -U sphinx
Collecting sphinx
  Using cached Sphinx-1.7.0-py2.py3-none-any.whl
Collecting alabaster<0.8,>=0.7 (from sphinx)
  Using cached alabaster-0.7.10-py2.py3-none-any.whl
Collecting packaging (from sphinx)
  Using cached packaging-16.8-py2.py3-none-any.whl
Collecting sphinxcontrib-websupport (from sphinx)
  Using cached sphinxcontrib_websupport-1.0.1-py2.py3-none-any.whl
Collecting imagesize (from sphinx)
  Downloading imagesize-1.0.0-py2.py3-none-any.whl
Collecting babel!=2.0,>=1.3 (from sphinx)
  Downloading Babel-2.5.3-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 168kB/s
Collecting setuptools (from sphinx)
  Downloading setuptools-38.5.1-py2.py3-none-any.whl (489kB)
    100% |████████████████████████████████| 491kB 1.3MB/s
Collecting snowballstemmer>=1.1 (from sphinx)
  Using cached snowballstemmer-1.2.1-py2.py3-none-any.whl
Collecting Jinja2>=2.3 (from sphinx)
  Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 3.3MB/s
Collecting requests>=2.0.0 (from sphinx)
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 2.0MB/s
Collecting six>=1.5 (from sphinx)
  Downloading six-1.11.0-py2.py3-none-any.whl
Collecting Pygments>=2.0 (from sphinx)
  Downloading Pygments-2.2.0-py2.py3-none-any.whl (841kB)
    100% |████████████████████████████████| 849kB 1.1MB/s
Collecting colorama>=0.3.5; sys_platform == "win32" (from sphinx)
  Downloading colorama-0.3.9-py2.py3-none-any.whl
Collecting docutils>=0.11 (from sphinx)
  Downloading docutils-0.14-py3-none-any.whl (543kB)
    100% |████████████████████████████████| 552kB 1.1MB/s
Collecting pyparsing (from packaging->sphinx)
  Using cached pyparsing-2.2.0-py2.py3-none-any.whl
Collecting pytz>=0a (from babel!=2.0,>=1.3->sphinx)
  Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 1.6MB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.3->sphinx)
Collecting idna<2.7,>=2.5 (from requests>=2.0.0->sphinx)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 3.9MB/s
Collecting urllib3<1.23,>=1.21.1 (from requests>=2.0.0->sphinx)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 1.6MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.0.0->sphinx)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 3.3MB/s
Collecting certifi>=2017.4.17 (from requests>=2.0.0->sphinx)
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
    100% |████████████████████████████████| 153kB 3.3MB/s
Installing collected packages: alabaster, six, pyparsing, packaging, sphinxcontrib-websupport, imagesize, pytz, babel, setuptools, snowballstemmer, MarkupSafe, Jinja2, idna, urllib3, chardet, certifi, requests, Pygm
ents, colorama, docutils, sphinx
  Found existing installation: six 1.10.0
    Uninstalling six-1.10.0:
      Successfully uninstalled six-1.10.0
  Found existing installation: setuptools 28.8.0
    Uninstalling setuptools-28.8.0:
      Successfully uninstalled setuptools-28.8.0
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Pygments-2.2.0 alabaster-0.7.10 babel-2.5.3 certifi-2018.1.18 chardet-3.0.4 colorama-0.3.9 docutils-0.14 idna-2.6 imagesize-1.0.0 packaging-16.8 pyparsing-2.2.0 pytz
-2018.3 requests-2.18.4 setuptools-38.5.1 six-1.11.0 snowballstemmer-1.2.1 sphinx-1.7.0 sphinxcontrib-websupport-1.0.1 urllib3-1.22

C:\Python34>

Nous avons installé Sphinx 1.7.0.

Vérifions :

C:\Python34\Scripts>sphinx-build --version
sphinx-build 1.7.0

C:\Python34\Scripts>

Les packages : “Installing collected packages: alabaster, six, pyparsing, packaging, sphinxcontrib-websupport, imagesize, pytz, babel, setuptools, snowballstemmer, MarkupSafe, Jinja2, idna, urllib3, chardet, certifi, requests, Pygments, colorama, docutils, sphinx …”

Ensuite vous vous rendez via cmd dans le sous-dossier Scripts où se trouvent maintenant :

  • sphinx-apidoc.exe
  • sphinx-autogen.exe
  • sphinx-build.exe
  • sphinx-quickstart.exe

Commande : sphinx-quickstart

C:\Python34\Scripts>sphinx-quickstart
usage: sphinx-quickstart [OPTIONS] 
sphinx-quickstart: error: the following arguments are required: PROJECT_DIR

C:\Python34\Scripts>

Il est demandé d’indiquer quel sera le dossier root (maître) du projet Sphinx. Le dossier sera automatiquement créé !

Ce que nous faisons :

sphinx-quickstart C:\Sphinx_projet

Je le place à la racine de C:\, vous pouvez choisir un autre emplacement.

Vous devez répondre à plusieurs questions :

Si je ne réponds pas aux questions, c’est que je laisse la valleur par défaut qui se trouve entre crochets [ ].

Je suppose que vous savez lire l’anglais comme moi  😉

  • Separate source and build directories (y/n) [n]: y
  • Name prefix for templates and static dir [_]:
  • Project name: Sphinx Documentation
  • Author name(s): Fabrice Dumont
  • Project release []: 1.0.0
  • Project language [en]: fr
  • Source file suffix [.rst]:
  • Name of your master document (without suffix) [index]:
  • Do you want to use the epub builder (y/n) [n]: y
  • autodoc: automatically insert docstrings from modules (y/n) [n]: y
  • doctest: automatically test code snippets in doctest blocks (y/n) [n]: y
  • intersphinx: link between Sphinx documentation of different projects (y/n) [n]: y
  • todo: write “todo” entries that can be shown or hidden on build (y/n) [n]: y
  • coverage: checks for documentation coverage (y/n) [n]: y
  • imgmath: include math, rendered as PNG or SVG images (y/n) [n]: y
  • mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
  • ifconfig: conditional inclusion of content based on config values (y/n) [n]: y
  • viewcode: include links to the source code of documented Python objects (y/n) [n]: y
  • githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: y

Note: imgmath and mathjax cannot be enabled at the same time. imgmath has been deselected.

A Makefile and a Windows command file can be generated for you so that you only have to run e.g. `make html’ instead of invoking sphinx-build directly.

  • Create Makefile? (y/n) [y]: y
  • Create Windows command file? (y/n) [y]: y

Les fichiers .rst sont éditable via le supperbe logiciel Notepad ++ ! reStructuredText

C:\Python34\Scripts>sphinx-quickstart C:\Sphinx_projet
Welcome to the Sphinx 1.7.0 quickstart utility.

Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).

Selected root path: C:\Sphinx_projet

You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/n) [n]: y

Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.
> Name prefix for templates and static dir [_]:

The project name will occur in several places in the built documentation.
> Project name: Sphinx Documentation
> Author name(s): Fabrice Dumont
> Project release []: 1.0.0

If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.

For a list of supported codes, see
http://sphinx-doc.org/config.html#confval-language.
> Project language [en]: fr

The file name suffix for source files. Commonly, this is either ".txt"
or ".rst".  Only files with this suffix are considered documents.
> Source file suffix [.rst]:

One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.
> Name of your master document (without suffix) [index]:

Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/n) [n]: y
Indicate which of the following Sphinx extensions should be enabled:
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> doctest: automatically test code snippets in doctest blocks (y/n) [n]: y
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]: y
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: y
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: y
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
> ifconfig: conditional inclusion of content based on config values (y/n) [n]: y
> viewcode: include links to the source code of documented Python objects (y/n) [n]: y
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: y
Note: imgmath and mathjax cannot be enabled at the same time. imgmath has been deselected.

A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.
> Create Makefile? (y/n) [y]: y
> Create Windows command file? (y/n) [y]: y

Creating file C:\Sphinx_projet\source\conf.py.
Creating file C:\Sphinx_projet\source\index.rst.
Creating file C:\Sphinx_projet\Makefile.
Creating file C:\Sphinx_projet\make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file C:\Sphinx_projet\source\index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
   make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.


C:\Python34\Scripts>

Tous ces parmètres se trouvent dans C:\Sphinx_projet\source\conf.py (pour moi puisque j’ai décidé que mon dossier root serait C:\Sphinx_projet)

L’installation s’est très bien passée. Nous vérifions dans CMD :

C:\>cd Sphinx_projet

C:\Sphinx_projet>dir
 Le volume dans le lecteur C s’appelle fabricedumont
 Le numéro de série du volume est 715B-DE3E

 Répertoire de C:\Sphinx_projet

18/02/2018  00:44              .
18/02/2018  00:44              ..
18/02/2018  00:44              build
18/02/2018  00:44               827 make.bat
18/02/2018  00:44               620 Makefile
18/02/2018  00:44              source
               2 fichier(s)            1.447 octets
               4 Rép(s)  51.089.014.784 octets libres

C:\Sphinx_projet>cd build

C:\Sphinx_projet\build>dir
 Le volume dans le lecteur C s’appelle fabricedumont
 Le numéro de série du volume est 715B-DE3E

 Répertoire de C:\Sphinx_projet\build

18/02/2018  00:44              .
18/02/2018  00:44              ..
               0 fichier(s)                0 octets
               2 Rép(s)  51.089.006.592 octets libres

C:\Sphinx_projet\build>cd ..

C:\Sphinx_projet>cd source

C:\Sphinx_projet\source>dir
 Le volume dans le lecteur C s’appelle fabricedumont
 Le numéro de série du volume est 715B-DE3E

 Répertoire de C:\Sphinx_projet\source

18/02/2018  00:44              .
18/02/2018  00:44              ..
18/02/2018  00:44             6.214 conf.py
18/02/2018  00:44               496 index.rst
18/02/2018  00:44              _static
18/02/2018  00:44              _templates
               2 fichier(s)            6.710 octets
               4 Rép(s)  51.088.474.112 octets libres

C:\Sphinx_projet\source>

Ouvrir et éditer le document index.rst avec notepad ++ qui se trouve pour moi dans C:\Sphinx_projet\source.

J’y ai ajouté les phrases suivantes :

Hello, ceci est mon premier document généré avec Sphinx !

Bon surf !

.. Sphinx Documentation documentation master file, created by
   sphinx-quickstart on Sun Feb 18 00:44:02 2018.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to Sphinx Documentation's documentation!
================================================
Hello, ceci est mon premier document généré avec Sphinx !

Bon surf !

.. toctree::
   :maxdepth: 2
   :caption: Contents:



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

Je lance ensuite la commande make html pour générer ma page html

C:\Sphinx_projet>make html
Running Sphinx v1.7.0
loading translations [fr]... done
making output directory...
loading pickled environment... not yet created
loading intersphinx inventory from https://docs.python.org/objects.inv...
intersphinx inventory has moved: https://docs.python.org/objects.inv -> https://docs.python.org/2/objects.inv
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in French (code: fr) ... done
dumping object inventory... done
build succeeded.

The HTML pages are in build\html.

C:\Sphinx_projet>

Première page générée avec Sphinx dans mon navigateur internet :

Et voilà ! 🙂

Vous pouvez directement choisir un thème qui vous plaît parmi ceux qui sont intégrés à l’installation de Sphinx.

Vous ouvrez dans un éditeur de textes comme Notepad ++ le document conf.py présent dans C:\Sphinx_projet\source pour mon installation.

Vous repérez la ligne html_theme = ‘alabaster’. Et vous changer son nom ! N’oubliez pas d’enregistrer avant de générer votre document html ! 🙂

D’après la documentation en ligne, il y a 10 thèmes builtin :

html_theme = ‘alabaster’

html_theme = ‘classic’

html_theme = ‘sphinxdoc’

html_theme = ‘scrolls’

html_theme = ‘agogo’

html_theme = ‘traditional’

html_theme = ‘nature’

html_theme = ‘haiku’ (mon préféré !)

html_theme = ‘pyramid’

html_theme = ‘bizstyle’

Pour en savoir plus sur les thèmes de Sphinx : http://www.sphinx-doc.org/en/stable/theming.html