6.4. Sphinx¶
| Reference | Topic |
|---|---|
| Sphinx | Conceptual explanation |
| Reference | Topic |
|---|---|
| Practical Sphinx presentation | Common usage |
| Project setup screencast | Start a project |
| Sphinx quickstart tutorial | Official tutorial |
Contents
6.4.1. Building documentation¶
Builders enable you to create both website and PDF documentation styles. Make sure you are using a6 for the below procedures
Contents
6.4.1.1. Manually¶
Per Carol Willing’s Practical Sphinx talk from PyCon 2018:
Activate the a6 environment from inside the documentation root directory if it is not already active
From the VS Code integrated terminal, build the HTML for documentation then start a server
make html python -m http.server
Open http://localhost:8000/_build/html/index.html in a web browser to view the website for documentation
You can update the .rst files and repeat the process, but don’t start another server (unless you want an HTTP socket error):
make html
Refresh the browser to see changes
Before committing, clear out the build:
make clean
Tip
You can automate this process if you want quick updates, like if you are proofreading documentation
6.4.1.2. Automatically¶
| Reference | Topic |
|---|---|
| sphinx-autobuild | Conceptual explanation |
| Reference | Topic |
|---|---|
| sphinx-autobuild | Official user manual |
Like in the manual build procedure, use the a6 environment from inside the documentation root directory via the VS Code integrated terminal:
sphinx-autobuild . _build/html -B -s 1- This should automatically open a web browser
- The server should be at http://127.0.0.1:8000
Use control-c to stop the server
Keep in mind:
- Once the server is running, saved changes to any .rst files should cause your web browser to update whatever part of the website you are viewing
- You will still need to manually navigate to the webpage you want to view
- If your web browser is set to a URL
that ends with
.html, the webpage will refresh in the same vertical position, but you may not be granted this luxury if the URL ends with something like.html#a-heading-you-clicked-on - For some reason, at least on a Mac, you may need to quit your web browser, stop sphinx-autobuild via control-c and then re-do the above before the auto-refresh behavior will work
Footnotes
| [1] | If you try to use no delay at all, -s 0, the
browser might not open |
6.4.1.3. PDF version¶
Note
Read the Docs will do this for you, but if you are so inclined it is possible to do it on your computer
-
OS What to get Mac MacTeX Windows Tex Live Linux Tex Live (probably) Use the VS Code integrated terminal from inside the documentation root directory, with the LaTeX builder:
make latex
Temporarily create a conda environment that you won’t need again
conda create -n PDF perl
Activate the PDF environment, then navigate to
_build/latex:make
Tip
You may need to do this a few times since it can take a few passes to resolve all the internal references. Just type enter if you get queried at all
6.4.2. Managing references¶
Contents
6.4.2.1. Using Intersphinx¶
| Reference | topic |
|---|---|
| Intersphinx | Conceptual explanation |
| Reference | Topic |
|---|---|
| sphinx.ext.intersphinx | Official documentation |
| Intersphinx reference syntax | Syntax explanation |
| Intersphinx inventory parser | For linking to large projects |
Locate the project’s objects.inv mapping, using the VS Code integrated terminal:
python -msphinx.ext.intersphinx http://www.sphinx-doc.org/en/master/objects.inv
You may have to experiment with the project base URL. Some common endings:
org/en/master/.io/en/latest/.com/en/latest/
Add the project’s base URL to the
intersphinx_mappingdictionary in conf.py:intersphinx_mapping = { 'python': ('https://docs.python.org/3', None), 'sphinx': ('http://www.sphinx-doc.org/en/master/', None), 'pytest': ('https://docs.pytest.org/en/latest/', None), 'rtfd': ('https://docs.readthedocs.io/en/latest/', None), 'rtd-sphinx-theme': ('https://sphinx-rtd-theme.readthedocs.io/en/latest/', None), ...
Inspect the objects.inv mapping from the project in question
- For large outputs, consider using a command line instead of the VS Code integrated terminal (but make sure to use a6)
Locate the desired target in the output and link to it using a corresponding role:
Referencing select outputs¶ Category in objects.inv Role to use std:doc:doc:rst:directive:rst:dir:std:label:ref:Webpages of documentation, under
std:doc, are arranged like the project’s table of contents, so you can figure out the role target from the URL that a browser displays for the particular webpage. Consider https://docs.python.org/3/tutorial/introduction.html:URL decomposition¶ Portion Interpretation In role target https://docs.python.org/3/Base URL python:tutorial/introduction.htmlDesired webpage tutorial/introductionYou can optionally define your own role title:
:doc:`python:tutorial/introduction`
:doc:`A most beauteous tutorial <python:tutorial/introduction>`
Add a role to documentation using the appropriate capitalization:
Read about Sphinx roles¶Read about :doc:`Sphinx roles <sphinx:usage/restructuredtext/roles>`
Note
When possible, use :ref: instead of :doc:, because the project’s
toctree may change
See also
Intersphinx with NumPy/Matplotlib has instructions for referencing NumPy and Matplotlib, though standard procedures from above are usually sufficient for AAAAAA
6.4.2.2. Referencing external links¶
For links that can not be managed with Intersphinx, use either xref or extlinks. In general you can use xref, but if the webpage you want to cite comes from a website that you often use, it makes sense to use extlinks:
Wikipedia articles, like https://en.wikipedia.org/wiki/Download:
RealPython tutorials, like https://realpython.com/python-type-checking:
Even Stack Overflow questions, like https://stackoverflow.com/questions/1441010/the-shortest-possible-output-from-git-log-containing-author-and-date:
:stack-q:`https://stackoverflow.com/questions/1441010/the-shortest-possible\ -output-from-git-log-containing-author-and-date <1441010/the-shortest-possible-output-from-git-log-containing-author-and-\ date>`
- Note that this works, but there may not be
syntax highlighting in the above
code-blockbecause of the\-escapes for new lines - This is still in compliance with line breaking standards
- Note that this works, but there may not be
syntax highlighting in the above
6.4.2.2.1. xref¶
| Reference | topic |
|---|---|
| xref | Conceptual explanation |
| Reference | Topic |
|---|---|
| Sphinx xref extension | User manual |
Add your URL to the
xref_linksdictionary in conf.py, below the delimeter-style comment that readsNew links below, sorted links abovexref_links = { 'Python': ('Python', 'https://www.python.org'), ... 'semver': ("Semantic Versioning", 'https://semver.org/'), # New links below, sorted links above 'ottobib': ('OttoBib', 'https://www.ottobib.com'), }
Add a link role to .rst files using the appropriate capitalization and an optional role title:
:xref:`Python.org <Python>`
6.4.2.2.2. extlinks¶
| Reference | topic |
|---|---|
| extlinks | Conceptual explanation |
| Reference | Topic |
|---|---|
| extlinks | Official documentation |
| Using a references extension | Related configuration and usage |
Add your base URL to the
extlinksdictionary in conf.py, with a%sat the end:extlinks = { 'wiki-pg': ('https://en.wikipedia.org/wiki/%s', ''), 'real-py': ('https://realpython.com/%s', ''), ... }
After you have added the base URL, you will then have access to a new custom role:
Note
The link checker is particular about capitalization for Wikipedia, so make sure to use the exact string from the end of the URL:
Download, notdownloadFor most websites other than Wikipedia, you will usually want to add in a role title:
Yields python-type-checking¶:real-py:`python-type-checking`
Yields type checking guide¶:real-py:`type checking guide <python-type-checking>`
Add a description of the URL to links, then add your custom role to documentation using the appropriate capitalization
Tip
Although you could use extlinks to create a URL that is not actually associated with a webpage, the link checking procedure will identify such errors
6.4.2.3. Checking links¶
Per Carol Willing’s Practical Sphinx talk from PyCon 2018:
From inside the documentation root directory, use the VS Code integrated terminal:
make linkcheck
6.4.2.4. Updating labels¶
With an active build running, open the VS Code integrated terminal from inside the documentation root directory
Use Intersphinx on
_build/html/objects.invto inspect inspect labels for AAAAAAVerify the proper label style
Update any labels via the VS Code Command Palette
- Search: Replace in Files
6.4.2.5. Referencing books¶
| Reference | Topic |
|---|---|
| BibTeX | Conceptual explanation |
| refs.bib | Collection of BibTeX-style citations |
| Reference | Topic |
|---|---|
| Book | Information source |
| BibTeX | Citation format |
| BibTeX extension | Converts BibTeX |
| OttoBib | Get BibTeX for your book |
| ISBN | Unique identifier for books |
| BibTeX Entry and Field Types | Syntax specifications |
| Convention for citing multiple authors | Use of et. al |
Check OttoBib for your ISBN and copy-paste the BibTeX option into refs.bib
Verify that you added a book entry in refs.bib
- A
bookentry requires at leastauthor(oreditor),title,publisher, andyearfields - Consider et. al conventions for multiple authors
- A
Add a role to books via
:cite:`bib-book-name`- Use a heading so that
toctreecan index the entry - Use a label that starts with
book-in books, and withbib-in refs.bib
.. _book-on-managing-yourself: ******************** On Managing Yourself ******************** .. csv-table:: :cite:`bib-on-managing-yourself` :align: center :header: Page(s), Topic
@Book{bib-on-managing-yourself, author = {Clayton M. Christensen et. al}, title = {HBR's 10 Must Reads: On Managing Yourself}, publisher = {Harvard Business Review Press}, year = {2010}, address = {Boston, Massachusetts}, isbn = {978-1-4221-5799-2} }- Use a heading so that
Tip
The BibTeX extension is unreceptive to role titles