-
Notifications
You must be signed in to change notification settings - Fork 76
docs: add how-to guide for sphinx autodocs extension #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
374b4da
docs: update how to index.rst add autodocs
AshleyCliff 5a28a15
docs: create autodocs.md
AshleyCliff dfee96a
fix: conf file formatting in autodocs.md
AshleyCliff a4cab00
fix: fix conf.py formatting in autodocs.md
AshleyCliff 329d966
fix: pr comment updates
AshleyCliff cee82c0
fix: requested pr changes
AshleyCliff 757bb65
fix: autodoc table formatting
AshleyCliff c050244
docs: update changelog for autodoc
AshleyCliff e280904
fix: autodoc spell fix
AshleyCliff c006f42
fix: autodoc formatting
AshleyCliff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| (sphinx-autodoc)= | ||
|
|
||
| # Import docstrings with Sphinx `autodoc` | ||
|
|
||
| Module and function details are useful reference material to have in documentation, but the process of manually pulling all the necessary details over can become tedious. The [Sphinx `autodoc` extension](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html) provides the capability to automatically pull in docstrings and module information for Python code. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| To use the Sphinx `autodoc` extension with the Starter Pack, you need: | ||
|
|
||
| * Python module files located within the same repository as your documentation | ||
|
|
||
| OR | ||
|
|
||
| * The code repository added as a [Git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) into the documentation repository | ||
|
|
||
| ## Setup | ||
|
|
||
| In the {file}`conf.py` file in your docs directory, update the `sys.path` so that Sphinx can find your module files. At the top of the file, add a `sys.path.insert` that adds your `<code>` directory: | ||
|
|
||
| ```{code-block} python | ||
| :caption: {file}`conf.py` | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| relative_code_path = Path('..', '<code>') | ||
| absolute_code_path = relative_code_path.resolve() | ||
| absolute_code_path_str = str(absolute_code_path) | ||
| sys.path.insert(0, absolute_code_path_str) # insert at index 0 so it occurs early in the list | ||
| ``` | ||
|
|
||
| Then, further down in the {file}`conf.py`, add `sphinx.ext.autodoc` to the list of extensions: | ||
|
|
||
| ```{code-block} python | ||
| :caption: {file}`conf.py` | ||
|
|
||
| extensions = [ | ||
| ... | ||
| "sphinx.ext.autodoc", | ||
| ] | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| See [Sphinx's `autodoc` instructions](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#usage) for details. | ||
|
|
||
| ## Known issues and limitations | ||
|
|
||
| There are a few issues and limitations that should be taken into consideration. | ||
|
|
||
| ### Language | ||
|
|
||
| The extension's usage is limited to Python code. There are extensions for some other languages but they have not been tested with the Starter Pack, such as [sphinxcontrib-rust](https://sphinxcontrib-rust.readthedocs.io/en/stable/) for Rust. | ||
|
|
||
| ### Docstring format | ||
|
|
||
| The `autodoc` extension pulls the docstrings straight into the the reStructuredText (reST) document, which requires the docstrings to be in reST format. For docstrings in the Numpy or Google style, the [napoleon](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#module-sphinx.ext.napoleon) extension can convert the docstrings into reST prior to processing by `autodoc`. | ||
|
|
||
| For documentation that is written in MyST Markdown, wrap the `eval-rst` directive around the `autodoc` calls: | ||
|
|
||
| ````{code-block} md | ||
|
|
||
| ```{eval-rst} | ||
|
|
||
| .. py:currentmodule:: code.<module-name> | ||
|
|
||
| .. automodule:: <module-name> | ||
|
akcano marked this conversation as resolved.
|
||
| :members: | ||
|
|
||
| ``` | ||
| ```` | ||
|
|
||
| ## Canonical examples | ||
|
|
||
| ```{list-table} Canonical autodoc examples | ||
| :header-rows: 1 | ||
|
|
||
| * - Product | ||
| - {file}`conf.py` | ||
| - Raw Doc | ||
| - Rendered Doc | ||
|
|
||
| * - Jubilant | ||
| - [conf.py](https://github.com/canonical/jubilant/blob/023ce73353352133c43dfb17b4a6cfad0f3e7816/docs/conf.py) | ||
| - [jubilant.rst](https://github.com/canonical/jubilant/blob/023ce73353352133c43dfb17b4a6cfad0f3e7816/docs/reference/jubilant.rst) | ||
| - [jubilant reference page](https://documentation.ubuntu.com/jubilant/reference/jubilant/) | ||
|
|
||
| * - Ops | ||
| - [conf.py](https://github.com/canonical/operator/blob/main/docs/conf.py) | ||
| - [ops.rst](https://github.com/canonical/operator/blob/main/docs/reference/ops.rst) | ||
| - [*ops* reference page](https://documentation.ubuntu.com/ops/latest/reference/ops/) | ||
|
|
||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.