Debian Codegraph @

doc/tutorial/automatic-doc-generation.rst

raw
1 Automatic documentation generation from code
2 ============================================
3
4 In the :ref:`previous section <tutorial-describing-objects>` of the tutorial
5 you manually documented a Python function in Sphinx. However, the description
6 was out of sync with the code itself, since the function signature was not
7 the same. Besides, it would be nice to reuse :pep:`Python docstrings
8 <257#what-is-a-docstring>` in the documentation, rather than having to write
9 the information in two places.
10
11 Fortunately, :doc:`the autodoc extension </usage/extensions/autodoc>` provides this
12 functionality.
13
14 Reusing signatures and docstrings with autodoc
15 ----------------------------------------------
16
17 To use autodoc, first add it to the list of enabled extensions:
18
19 .. code-block:: python
20 :caption: docs/source/conf.py
21 :emphasize-lines: 4
22
23 extensions = [
24 'sphinx.ext.duration',
25 'sphinx.ext.doctest',
26 'sphinx.ext.autodoc',
27 ]
28
29 Next, move the content of the ``.. py:function`` directive to the function
30 docstring in the original Python file, as follows:
31
32 .. code-block:: python
33 :caption: lumache.py
34 :emphasize-lines: 2-11
35
36 def get_random_ingredients(kind=None):
37 """
38 Return a list of random ingredients as strings.
39
40 :param kind: Optional "kind" of ingredients.
41 :type kind: list[str] or None
42 :raise lumache.InvalidKindError: If the kind is invalid.
43 :return: The ingredients list.
44 :rtype: list[str]
45
46 """
47 return ["shells", "gorgonzola", "parsley"]
48
49 Finally, replace the ``.. py:function`` directive from the Sphinx documentation
50 with :rst:dir:`autofunction`:
51
52 .. code-block:: rst
53 :caption: docs/source/usage.rst
54 :emphasize-lines: 3
55
56 you can use the ``lumache.get_random_ingredients()`` function:
57
58 .. autofunction:: lumache.get_random_ingredients
59
60 If you now build the HTML documentation, the output will be the same!
61 With the advantage that it is generated from the code itself.
62 Sphinx took the reStructuredText from the docstring and included it,
63 also generating proper cross-references.
64
65 You can also autogenerate documentation from other objects. For example, add
66 the code for the ``InvalidKindError`` exception:
67
68 .. code-block:: python
69 :caption: lumache.py
70
71 class InvalidKindError(Exception):
72 """Raised if the kind is invalid."""
73 pass
74
75 And replace the ``.. py:exception`` directive with :rst:dir:`autoexception`
76 as follows:
77
78 .. code-block:: rst
79 :caption: docs/source/usage.rst
80 :emphasize-lines: 4
81
82 or ``"veggies"``. Otherwise, :py:func:`lumache.get_random_ingredients`
83 will raise an exception.
84
85 .. autoexception:: lumache.InvalidKindError
86
87 And again, after running ``make html``, the output will be the same as before.
88
89 Generating comprehensive API references
90 ---------------------------------------
91
92 While using ``sphinx.ext.autodoc`` makes keeping the code and the documentation
93 in sync much easier, it still requires you to write an ``auto*`` directive
94 for every object you want to document. Sphinx provides yet another level of
95 automation: the :doc:`autosummary </usage/extensions/autosummary>` extension.
96
97 The :rst:dir:`autosummary` directive generates documents that contain all the
98 necessary ``autodoc`` directives. To use it, first enable the autosummary
99 extension:
100
101 .. code-block:: python
102 :caption: docs/source/conf.py
103 :emphasize-lines: 5
104
105 extensions = [
106 'sphinx.ext.duration',
107 'sphinx.ext.doctest',
108 'sphinx.ext.autodoc',
109 'sphinx.ext.autosummary',
110 ]
111
112 Next, create a new ``api.rst`` file with these contents:
113
114 .. code-block:: rst
115 :caption: docs/source/api.rst
116
117 API
118 ===
119
120 .. autosummary::
121 :toctree: generated
122
123 lumache
124
125 Remember to include the new document in the root toctree:
126
127 .. code-block:: rst
128 :caption: docs/source/index.rst
129 :emphasize-lines: 7
130
131 Contents
132 --------
133
134 .. toctree::
135
136 usage
137 api
138
139 Finally, after you build the HTML documentation running ``make html``, it will
140 contain two new pages:
141
142 - ``api.html``, corresponding to ``docs/source/api.rst`` and containing a table
143 with the objects you included in the ``autosummary`` directive (in this case,
144 only one).
145 - ``generated/lumache.html``, corresponding to a newly created reStructuredText file
146 ``generated/lumache.rst`` and containing a summary of members of the module,
147 in this case one function and one exception.
148
149 .. figure:: /_static/tutorial/lumache-autosummary.png
150 :width: 80%
151 :align: center
152 :alt: Summary page created by autosummary
153
154 Summary page created by autosummary
155
156 Each of the links in the summary page will take you to the places where you
157 originally used the corresponding ``autodoc`` directive, in this case in the
158 ``usage.rst`` document.
159
160 .. note::
161
162 The generated files are based on `Jinja2
163 templates <https://jinja.palletsprojects.com/>`_ that
164 :ref:`can be customized <autosummary-customizing-templates>`,
165 but that is out of scope for this tutorial.