|
| 1 | +About |
| 2 | +===== |
| 3 | + |
| 4 | +`sphinx-autodoc` is almost, but not quite, entirely unlike `sphinx-apidoc` and |
| 5 | +`sphinx-autogen` provided with Sphinx. It is a tool to automatically |
| 6 | +create `.rst` source files, from which Sphinx can create API |
| 7 | +documentation. |
| 8 | + |
| 9 | +It does this by walking thru a Python package and generating rst files |
| 10 | +for: |
| 11 | + |
| 12 | +* Full API doc. Each module will be treated by the [autosummary] |
| 13 | + extension `sphinx.ext.autosummary`. Files are written to |
| 14 | + `source/generated/api` by default. Module doc strings (if present) are also |
| 15 | + included. |
| 16 | +* Detect and include hand written docs, by default in `source/written/`. |
| 17 | + |
| 18 | +Install |
| 19 | +======= |
| 20 | + |
| 21 | +```sh |
| 22 | +$ git clone ... |
| 23 | +$ cd sphinx-autodoc |
| 24 | +$ pip install [-e] . |
| 25 | +``` |
| 26 | + |
| 27 | +Usage tl;dr |
| 28 | +=========== |
| 29 | + |
| 30 | +```sh |
| 31 | +$ cd myproject/doc |
| 32 | +$ sphinx-autodoc myproject |
| 33 | +$ make html |
| 34 | +$ firefox build/html/index.html |
| 35 | +``` |
| 36 | + |
| 37 | +Examples |
| 38 | +======== |
| 39 | + |
| 40 | +We provide a minimal, self-contained Python package with a doc dir for |
| 41 | +experimentation: `example_package/autodoctest`. In particular, check out |
| 42 | +`example_package/autodoctest/doc/generate-apidoc.sh`. |
| 43 | + |
| 44 | +```sh |
| 45 | +$ cd example_package/autodoctest/ |
| 46 | +$ pip install -e . |
| 47 | +$ cd doc |
| 48 | +$ ./generate-apidoc.sh |
| 49 | +$ firefox build/html/index.html |
| 50 | +``` |
| 51 | + |
| 52 | +Usage |
| 53 | +===== |
| 54 | + |
| 55 | +Set up doc dir using `sphinx-quickstart` |
| 56 | +---------------------------------------- |
| 57 | + |
| 58 | +If you haven't already, create a sphinx doc dir for your project (lets assume |
| 59 | +`myproject/doc` with the source dir `myproject/doc/source` and the main index |
| 60 | +file `myproject/doc/source/index.rst`). |
| 61 | + |
| 62 | +```sh |
| 63 | +$ cd myproject/doc |
| 64 | +$ sphinx-quickstart |
| 65 | +``` |
| 66 | + |
| 67 | +Choose a separate source and build dir (e.g. `doc/source` and |
| 68 | +`doc/build`). Accept most other defaults. |
| 69 | + |
| 70 | +Configuration |
| 71 | +------------- |
| 72 | + |
| 73 | +Inspect `doc/source/conf.py` and make sure you have at least these |
| 74 | +extensions enabled. |
| 75 | + |
| 76 | +```py |
| 77 | +extensions = [ |
| 78 | + 'sphinx.ext.autodoc', |
| 79 | + 'sphinx.ext.autosummary', |
| 80 | + 'sphinx.ext.napoleon', |
| 81 | + 'sphinx.ext.viewcode', |
| 82 | + ] |
| 83 | +``` |
| 84 | + |
| 85 | +We use the [napoleon] extension instead of [numpydoc]. See |
| 86 | +`example_package/autodoctest/doc/source/conf.py` for more extensions. |
| 87 | + |
| 88 | +Now modify `doc/source/conf.py` to include these configs. |
| 89 | + |
| 90 | +```py |
| 91 | +autosummary_generate = True |
| 92 | + |
| 93 | +autodoc_default_options = { |
| 94 | + 'members': True, |
| 95 | + # The ones below should be optional but work nicely together with |
| 96 | + # example_package/autodoctest/doc/source/_templates/autosummary/class.rst |
| 97 | + # and other defaults in sphinx-autodoc. |
| 98 | + 'show-inheritance': True, |
| 99 | + 'inherited-members': True, |
| 100 | + 'no-special-members': True, |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +See also `example_package/autodoctest/doc/source/conf.py`. |
| 105 | + |
| 106 | + |
| 107 | +Notes for numpydoc, class template |
| 108 | +---------------------------------- |
| 109 | + |
| 110 | +We used to use [numpydoc] and numpy's class template in the past. However, with |
| 111 | +recent Sphinx versions, the [napoleon] extension does a great job. Use our own |
| 112 | +class template if you like in conjunction with that: copy |
| 113 | +`example_package/autodoctest/doc/source/_templates/autosummary/class.rst` to |
| 114 | +your `doc/source/_templates/autosummary/`. |
| 115 | + |
| 116 | +Use sphinx-autodoc |
| 117 | +------------------ |
| 118 | + |
| 119 | +tl;dr: See `example_package/autodoctest/doc/generate-apidoc.sh` |
| 120 | + |
| 121 | + |
| 122 | +Now walk thru the package and create rst files. We use `-i` to create an |
| 123 | +initial `source/index.rst`. |
| 124 | + |
| 125 | +```sh |
| 126 | +$ sphinx-autodoc -i -s doc/source myproject |
| 127 | +``` |
| 128 | + |
| 129 | +Note that this will overwrite an existing `index.rst` file (a backup is |
| 130 | +made however). |
| 131 | + |
| 132 | +You can run the script from anywhere, provided that `myproject` is a |
| 133 | +Python package since we need to import it to inspect all its subpackages |
| 134 | +and modules. The source path `-s` must point to the dir where you want |
| 135 | +all rst files to be generated, which will usually be `doc/source/`. |
| 136 | + |
| 137 | +Now modify `source/index.rst` to suit you needs and then run `make html` |
| 138 | +using the Makefile generated by `sphinx-quickstart` earlier. Inspect the |
| 139 | +rendered docs. |
| 140 | + |
| 141 | +```sh |
| 142 | +$ firefox build/html/index.html |
| 143 | +``` |
| 144 | + |
| 145 | +[numpydoc]: https://numpydoc.readthedocs.io |
| 146 | +[napoleon]: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html |
| 147 | +[autosummary]: https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html |
0 commit comments