|
| 1 | +Documentation |
| 2 | +============= |
| 3 | + |
| 4 | +We will be using a `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation from |
| 5 | +docstrings that are written within code. Docstrings are long-form |
| 6 | +comments beneath class and functions declaration that typically describe: |
| 7 | + |
| 8 | +- What the function or class does. |
| 9 | +- Its inputs and their data types. |
| 10 | +- Its outputs and their data types. |
| 11 | + |
| 12 | +For example: |
| 13 | + |
| 14 | +.. code:: Python |
| 15 | +
|
| 16 | + def add(a, b): |
| 17 | + """ |
| 18 | + Add two integers. |
| 19 | + |
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + a : int |
| 23 | + First integer. |
| 24 | + b : int |
| 25 | + Second integer. |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + result : int |
| 30 | + Sum of inputs. |
| 31 | +
|
| 32 | + """ |
| 33 | +
|
| 34 | + assert isinstance(a, int) |
| 35 | + assert isinstance(b, int) |
| 36 | + return a + b |
| 37 | +
|
| 38 | +Which will be rendered as :func:`pydevtips.utils.add`. |
| 39 | + |
| 40 | +Initial setup |
| 41 | +------------- |
| 42 | + |
| 43 | +So you've diligently written your docstrings (or used GitHub Copilot!), and you |
| 44 | +want to generate your documentation. |
| 45 | + |
| 46 | +Here are some recommended steps to get started with Sphinx: |
| 47 | + |
| 48 | +#. Create a lightweight virtual environment for building the documentation. This can save a lot of time when building and publishing documentation remotely, as we'll show with `ReadTheDocs <https://readthedocs.org/>`__. |
| 49 | + |
| 50 | + .. code:: bash |
| 51 | + |
| 52 | + # create new environment, press enter to accept |
| 53 | + conda create -n docs_env python=3.9 |
| 54 | +
|
| 55 | + # activate environment |
| 56 | + conda activate docs_env |
| 57 | +
|
| 58 | + # install requirements stored in `docs` |
| 59 | + # (may differ from our file depending on your needs) |
| 60 | + (docs_env) cd docs |
| 61 | + (docs_env) pip install -r requirements.txt |
| 62 | +
|
| 63 | +#. Inside your virtual environment, run ``sphinx-quickstart`` to creates a lot of the boilerplate configurations. This will guide you through a set of questions, such as your project details. We recommend creating separate ``source`` and ``build`` directories. |
| 64 | +#. Build the documentation, e.g. with ``make html``. |
| 65 | +#. Open ``docs/build/html/index.html`` in a browser to see your initial documentation! |
| 66 | + |
| 67 | +Editing your documentation |
| 68 | +-------------------------- |
| 69 | + |
| 70 | +The ``index.rst`` file will serve as the "homepage" for your documentation (built into ``index.html``). |
| 71 | +Typically, people have the same content as their README. Before you copy-and-paste |
| 72 | +the contents (!), you can directly insert the contents with the following line. |
| 73 | + |
| 74 | +.. code:: rst |
| 75 | +
|
| 76 | + .. include:: ../../README.rst |
| 77 | +
|
| 78 | +.. note:: |
| 79 | + |
| 80 | + `reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`__ |
| 81 | + is the default plaintext markup language used by Sphinx. At this point, you may be thinking: |
| 82 | + *"But my README is a Markdown file (.md)..."*. While there are `tools <https://www.sphinx-doc.org/en/master/usage/markdown.html>`__ |
| 83 | + to make Sphinx compatible with Markdown, I think you will save yourself more headaches to simply |
| 84 | + switch to reStructuredText. There are also `online tools <https://cloudconvert.com/md-to-rst>`__ |
| 85 | + to help you with that. |
| 86 | + |
| 87 | + |
| 88 | +Adding new pages to your documentation amount to: |
| 89 | + |
| 90 | +#. Creating new RST files. |
| 91 | +#. Including them in your ``index.rst`` file. |
| 92 | +#. Rebuilding the documentation, e.g. with ``make html``. |
| 93 | + |
| 94 | +You may also need to edit the ``conf.py`` file to use different features. |
| 95 | +Check out our `index.rst <https://raw.githubusercontent.com/ebezzam/python-dev-tips/feat/docs/docs/source/index.rst>`__ |
| 96 | +and `conf.py <https://github.com/ebezzam/python-dev-tips/blob/feat/docs/docs/source/conf.py>`__ |
| 97 | +files for example configurations. |
| 98 | + |
| 99 | + |
| 100 | +You can do a clean build of your documentation with the following commands: |
| 101 | + |
| 102 | +.. code:: bash |
| 103 | +
|
| 104 | + make clean |
| 105 | + make html |
| 106 | +
|
| 107 | +
|
| 108 | +Pro-tips |
| 109 | +-------- |
| 110 | + |
| 111 | +* Changing to the ReadTheDocs theme inside `conf.py <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L75>`__. |
| 112 | +* `Intersphinx <https://docs.readthedocs.io/en/stable/guides/intersphinx.html>`__ for linking to other documentations. |
| 113 | + In the ``conf.py`` file: `add <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L43>`__ |
| 114 | + the Sphinx extension, and `link <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L54>`__ |
| 115 | + to the other documentation. Inside your documentation you can link to the other library, e.g. |
| 116 | + for data types: |
| 117 | + |
| 118 | + .. code:: Python |
| 119 | +
|
| 120 | + ... |
| 121 | +
|
| 122 | + """ |
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + filter : :py:class:`~numpy.ndarray` |
| 126 | + """ |
| 127 | +
|
| 128 | + ... |
| 129 | +
|
| 130 | + which renders as in :func:`pydevtips.fftconvolve.RFFTConvolve.__init__` |
| 131 | + with a clickable link to NumPy's documentation. |
| 132 | +* `Mock modules <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L24>`__ to keep your documentation virtual environment light. |
| 133 | +* `Add the path <https://github.com/ebezzam/python-dev-tips/blob/feat/docs/docs/source/conf.py#L22>`__ |
| 134 | + to your package, so that it doesn't have to be installed (again keeping your documentation environment light!). |
| 135 | +* `Automate year <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L32>`__. |
| 136 | + |
| 137 | + |
| 138 | +Publishing |
| 139 | +---------- |
| 140 | + |
| 141 | +With a set of HTML files, there are many ways to publish your documentation online. |
| 142 | +We present one approach through `ReadTheDocs <https://readthedocs.org/>`__ (RTD), which is |
| 143 | +free and very popular among Python developers. Another popular free options is through |
| 144 | +`GitHub Pages <https://pages.github.com/>`__. I prefer RTD to not have the GitHub username or |
| 145 | +organization in the documentation URL. |
| 146 | + |
| 147 | +To publish on RTD: |
| 148 | + |
| 149 | +#. Make an account: https://readthedocs.org/accounts/signup/ |
| 150 | +#. Import a project from the `dashboard <https://readthedocs.org/dashboard/>`__. There are two ways to do this: (1) linking your GitHub account and selecting one of your **public** repositories, or (2) importing the project manually. When linking to GitHub, the documentation is re-built whenever there are changes to the selected branch. |
| 151 | + |
| 152 | +You can (optionally) define a `.readthedocs.yaml <>`__ |
| 153 | +file to ensure a build environment as close as possible to your local machine. |
0 commit comments