Embed a DOT graph with GraphViz in a Sphinx-generated page

Graphes?

I am showing here an « experimental » Sphinx feature, to easily embed graphs written in plain text with a rST tag .. graphviz::, and converted to nice images with the world-famous software GraphViz.

This feature is provided by the sphinx.ext.graphviz extension, included by default in sphinx since its version 0.6.

This directive graphviz uses the DOT Graph language to describe a graph (directed or not). This language is a world-standard in Computer Science, everyone uses it: engineers, researchers, professors etc.

Avertissement

If the following graphs are not well displayed, that is probably because your browser does not support the SVG format. Try to use Mozilla Firefox instead.

Examples

.. graphviz:::

Sphinx is a software than basically implements these conversion steps:

digraph Sphinx { "rST text files (.rst)" -> "web pages (.html)"; "rST text files (.rst)" -> "man pages (.1)"; "rST text files (.rst)" -> "LaTeX files (.tex)"; "rST text files (.rst)" -> "web pages (.epub)"; }

The reStructuredText code is simple and contains the semantic information about the graph, not graphical/vectorial/pixel information:

.. graphviz::

   digraph Sphinx {
      "rST text files (.rst)" -> "web pages (.html)";
      "rST text files (.rst)" -> "man pages (.1)";
      "rST text files (.rst)" -> "LaTeX files (.tex)";
      "rST text files (.rst)" -> "web pages (.epub)";
   }

.. digraph:::

We can also write directed graphs.

For instance Sphinx autodoc (and a script like pytorst.py) implements this conversion step:

digraph autodoc { "Python file (.py)" -> "rST text file (.rst)" -> "web pages (.html)"; }

.. graph:::

This directive is for non-directed graphs.

LaTeX

pdflatex implements this conversion (for example with my resume, written in LaTeX):

graph latex { "LaTeX file" -- "PDF file" ".tex" -- ".pdf"; "cv.en.tex" -- "cv.en.pdf"; }

HeVeA does another transformation:

graph hevea { "LaTeX file" -- "web page (HTML)" ".tex" -- ".html"; "cv.tex" -- "cv.hevea.html"; }

The reStructuredText code is again simple and contains the semantic information about the graph, not graphical/vectorial/pixel information:

.. graph:: latex

   "LaTeX file" -- "PDF file"
   ".tex" -- ".pdf";
   "cv.en.tex" -- "cv.en.pdf";

The « Hautes-Alpes » region in France

I was born and I grew up in Briançon, in the Hautes-Alpes, until I was 16, before leaving to study at the Lycée Thiers in Marseille.

In the Hautes-Alpes, the main cities are the following (an edge means that the two towns are in the same valley ie. connected with a river!):

graph alpes { "Bri" [color="red", style="bold", label="Briançon"]; "Veynes" -- "Gap" -- "Chorges" -- "Embrun" -- "Mont-Dauphin" -- "L'Argentière" -- "Bri"; "Mont-Dauphin" -- "Guillestre"; "L'Argentière" -- "Valouise" -- "Puit St-Vincent"; "Bri" -- "Montgenèvre"; "Embrun" -- "Les Orres"; }

An external file

This rST command can also automatically fetch an external file. For instance here is a dependance graph (for an old network game project I wrote in Python back in 2012) generated via pyreverse, and included here with .. graphviz:: .graph.dot (the file is called .graph.dot, you can download it and view it on a text editor).

digraph "packages_Bomberman" { charset="utf-8" rankdir=BT "3" [shape="octagon", color="yellow", style="bold" label="PyRlwrap"]; "4" [shape="egg", color="red", style="bold" label="ParseMessageIn"]; "6" [shape="box", color="blue", style="bold" label="Bomb"]; "9" [color="green", style="bold" label="BombermanServer"]; "10" [shape="box", color="blue", style="bold" label="Matrix"]; "12" [shape="box", color="blue", style="bold" label="Bonus"]; "14" [color="green", style="bold" label="BombermanClient"]; "15" [shape="house", color="magenta", style="bold" label="KeyBinding"]; "17" [shape="house", color="magenta", style="bold" label="AffichPygame"]; "19" [shape="box", color="blue", style="bold" label="Player"]; "23" [shape="house", color="magenta", style="bold" label="SimpleGame"]; "24" [color="green", style="bold" label="ConfigClient"]; "25" [shape="box", color="blue", style="bold" label="Board"]; "29" [shape="octagon", color="yellow", style="bold" label="conf"]; "30" [shape="hexagon", color="black", style="bold" label="ANSIColors"]; "31" [color="green", style="bold" label="IA_Bomberman"]; "32" [shape="egg", color="red", style="bold" label="ParseMessageOut"]; "33" [shape="hexagon", color="black", style="bold" label="ParseCommandArgs"]; "34" [shape="octagon", color="yellow", style="bold" label="scanf"]; "44" [shape="box", label="Constants"]; "45" [color="green", style="bold" label="ConfigServer"]; "4" -> "34" [arrowtail="none", arrowhead="open"]; "4" -> "30" [arrowtail="none", arrowhead="open"]; "4" -> "44" [arrowtail="none", arrowhead="open"]; "6" -> "30" [arrowtail="none", arrowhead="open"]; "6" -> "44" [arrowtail="none", arrowhead="open"]; "9" -> "10" [arrowtail="none", arrowhead="open"]; "9" -> "19" [arrowtail="none", arrowhead="open"]; "9" -> "30" [arrowtail="none", arrowhead="open"]; "9" -> "4" [arrowtail="none", arrowhead="open"]; "9" -> "25" [arrowtail="none", arrowhead="open"]; "9" -> "33" [arrowtail="none", arrowhead="open"]; "9" -> "32" [arrowtail="none", arrowhead="open"]; "9" -> "17" [arrowtail="none", arrowhead="open"]; "9" -> "44" [arrowtail="none", arrowhead="open"]; "9" -> "45" [arrowtail="none", arrowhead="open"]; "12" -> "44" [arrowtail="none", arrowhead="open"]; "14" -> "10" [arrowtail="none", arrowhead="open"]; "14" -> "19" [arrowtail="none", arrowhead="open"]; "14" -> "30" [arrowtail="none", arrowhead="open"]; "14" -> "4" [arrowtail="none", arrowhead="open"]; "14" -> "25" [arrowtail="none", arrowhead="open"]; "14" -> "33" [arrowtail="none", arrowhead="open"]; "14" -> "6" [arrowtail="none", arrowhead="open"]; "14" -> "32" [arrowtail="none", arrowhead="open"]; "14" -> "17" [arrowtail="none", arrowhead="open"]; "14" -> "44" [arrowtail="none", arrowhead="open"]; "14" -> "24" [arrowtail="none", arrowhead="open"]; "15" -> "30" [arrowtail="none", arrowhead="open"]; "17" -> "19" [arrowtail="none", arrowhead="open"]; "17" -> "23" [arrowtail="none", arrowhead="open"]; "17" -> "4" [arrowtail="none", arrowhead="open"]; "17" -> "32" [arrowtail="none", arrowhead="open"]; "17" -> "44" [arrowtail="none", arrowhead="open"]; "19" -> "6" [arrowtail="none", arrowhead="open"]; "19" -> "30" [arrowtail="none", arrowhead="open"]; "19" -> "32" [arrowtail="none", arrowhead="open"]; "19" -> "44" [arrowtail="none", arrowhead="open"]; "23" -> "10" [arrowtail="none", arrowhead="open"]; "23" -> "19" [arrowtail="none", arrowhead="open"]; "23" -> "30" [arrowtail="none", arrowhead="open"]; "23" -> "15" [arrowtail="none", arrowhead="open"]; "23" -> "25" [arrowtail="none", arrowhead="open"]; "23" -> "33" [arrowtail="none", arrowhead="open"]; "23" -> "32" [arrowtail="none", arrowhead="open"]; "23" -> "17" [arrowtail="none", arrowhead="open"]; "23" -> "44" [arrowtail="none", arrowhead="open"]; "25" -> "6" [arrowtail="none", arrowhead="open"]; "25" -> "10" [arrowtail="none", arrowhead="open"]; "25" -> "30" [arrowtail="none", arrowhead="open"]; "25" -> "32" [arrowtail="none", arrowhead="open"]; "25" -> "12" [arrowtail="none", arrowhead="open"]; "25" -> "44" [arrowtail="none", arrowhead="open"]; "31" -> "14" [arrowtail="none", arrowhead="open"]; "32" -> "30" [arrowtail="none", arrowhead="open"]; "32" -> "44" [arrowtail="none", arrowhead="open"]; "44" -> "15" [arrowtail="none", arrowhead="open"]; "44" -> "30" [arrowtail="none", arrowhead="open"]; }


Using this for the scipy doc

For scipy scipy.sparse.csgraph module, its documentation shows two small graphs, initially written in ASCII, but I wanted to try to add nice SVG files instead.

I opened the issue (#5344), this comment is related also. This pull request (#5345) was not interesting: the raw SVG included in the rST page is not working for PDF output neither docstring inspection (with IPython or other).

Graph G1:

Graph G1 0 0 1 1 0--1 2 2 2 0--2 1

Graph G2:

Graph G2 0 0 1 1 0--1 2 2 2 0--2 0

Another extension: sphinx.ext.todolist

This page also tests the feature provided by the sphinx.ext.todolist extension. This extension adds a directive .. todo:: which allows to add a TODO, i.e. one thing to do, to fix or to work on.

À faire

How to use this from a MyST or Markdown page in Sphinx?

And then, you can display a TODO list with the directive .. totolist::, like I do in the todo.html page.