2

So I've been trying to get Sphinx to generate inheritance diagrams for all submodules / classes when generating .rst files through sphinx-apidoc, similar to this example. But I have not found any built-in features to do this in Sphinx.

The nearest solution I found was to manually add an inheritance diagram line with the class name to each class docstring in code, but this is not very convenient.

So the question is how do I add inheritance diagrams to all module .rst files?

joneivinds
  • 61
  • 5
  • If you have a question and its solution, it is fine to post both. But don't include the solution in the question. First post the question, then the answer. That is how Stack Overflow works (welcome, by the way!). – mzjn Dec 12 '19 at 17:25
  • See also https://meta.stackoverflow.com/q/250204/407651 – mzjn Dec 12 '19 at 17:42
  • Yeah first post, thanks for the input :) Will do that next time! – joneivinds Dec 13 '19 at 07:45
  • Don't wait until next time. You should remove the "answer part" of this question and put it in a proper answer. – mzjn Dec 13 '19 at 08:08

1 Answers1

4

Found a solution to the problem:

  1. Go to the folder /usr/local/lib/python3.6/dist-packages/sphinx/templates/apidoc/
  2. Open package.rst_t
  3. After *{{- [submodule, "module"] | join(" ") | e | heading(2) }}* add:

    .. inheritance-diagram:: {{submodule}}
    
       :parts: 1
    

You also need to add these extentions in the conf.py file when making the html files:

extensions = ['sphinx.ext.graphviz','sphinx.ext.inheritance_diagram']

To make the graphs more readable I also added this to conf.py (TB=Top to bottom view):

inheritance_graph_attrs = dict(rankdir="TB", size='""')
joneivinds
  • 61
  • 5