@@ -86,3 +86,81 @@ as follows:
8686 .. autoexception:: lumache.InvalidKindError
8787
8888 And again, after running ``make html ``, the output will be the same as before.
89+
90+ Generating comprehensive API references
91+ ---------------------------------------
92+
93+ While using ``sphinx.ext.autodoc `` makes keeping the code and the documentation
94+ in sync much easier, it still requires you to write an ``auto* `` directive
95+ for every object you want to document. Sphinx provides yet another level of
96+ automation: the :doc: `autosummary </usage/extensions/autosummary >` extension.
97+
98+ The :rst:dir: `autosummary ` directive generates documents that contain all the
99+ necessary ``autodoc `` directives. To use it, first enable the autosummary
100+ extension:
101+
102+ .. code-block :: python
103+ :caption: docs/ source/ conf.py
104+ :emphasize- lines: 5
105+
106+ extensions = [
107+ ' sphinx.ext.duration' ,
108+ ' sphinx.ext.doctest' ,
109+ ' sphinx.ext.autodoc' ,
110+ ' sphinx.ext.autosummary' ,
111+ ]
112+
113+ Next, create a new ``api.rst `` file with these contents:
114+
115+ .. code-block :: rst
116+ :caption: docs/source/api.rst
117+
118+ API
119+ ===
120+
121+ .. autosummary::
122+ :toctree: generated
123+
124+ lumache
125+
126+ Remember to include the new document in the root toctree:
127+
128+ .. code-block :: rst
129+ :caption: docs/source/index.rst
130+ :emphasize-lines: 7
131+
132+ Contents
133+ --------
134+
135+ .. toctree::
136+
137+ usage
138+ api
139+
140+ Finally, after you build the HTML documentation running ``make html ``, it will
141+ contain two new pages:
142+
143+ - ``api.html ``, corresponding to ``docs/source/api.rst `` and containing a table
144+ with the objects you included in the ``autosummary `` directive (in this case,
145+ only one).
146+ - ``generated/lumache.html ``, corresponding to a newly created reST file
147+ ``generated/lumache.rst `` and containing a summary of members of the module,
148+ in this case one function and one exception.
149+
150+ .. figure :: /_static/tutorial/lumache-autosummary.png
151+ :width: 80%
152+ :align: center
153+ :alt: Summary page created by autosummary
154+
155+ Summary page created by autosummary
156+
157+ Each of the links in the summary page will take you to the places where you
158+ originally used the correspoding ``autodoc `` directive, in this case in the
159+ ``usage.rst `` document.
160+
161+ .. note ::
162+
163+ The generated files are based on `Jinja2
164+ templates <https://jinja2docs.readthedocs.io/> `_ that
165+ :ref: `can be customized <autosummary-customizing-templates >`,
166+ but that is out of scope for this tutorial.
0 commit comments