Skip to content

Commit 47c2218

Browse files
committed
Add documentation subpage.
1 parent ec6b15c commit 47c2218

File tree

9 files changed

+226
-73
lines changed

9 files changed

+226
-73
lines changed

.readthedocs.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Set the version of Python and other tools you might need
9+
build:
10+
os: ubuntu-20.04
11+
tools:
12+
python: "3.9"
13+
14+
# Build documentation in the docs/ directory with Sphinx
15+
sphinx:
16+
configuration: docs/source/conf.py
17+
18+
# If using Sphinx, optionally build your docs in additional formats such as PDF
19+
# formats:
20+
# - pdf
21+
22+
# Optionally declare the Python requirements required to build your docs
23+
python:
24+
install:
25+
- requirements: docs/requirements.txt

README.rst

-65
Original file line numberDiff line numberDiff line change
@@ -90,70 +90,6 @@ To run a specific test:
9090
(project_env) pytest tests/test_fftconvolve.py::test_fft
9191
9292
93-
Documentation
94-
=============
95-
96-
Will be using `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation.
97-
98-
#. Write docstrings when coding. Great if already started!
99-
100-
First time only steps
101-
#. (Recommended) create a lightweight virtual environment for building the documentation. Can save a lot of time when building docs remotely, as we'll show with ReadTheDocs
102-
103-
.. code:: bash
104-
105-
# create new environment, press enter to accept
106-
conda create -n docs_env python=3.9
107-
108-
# activate environment
109-
conda activate docs_env
110-
111-
# install requirements
112-
(docs_env) pip install -e .
113-
(docs_env) cd docs
114-
(docs_env) pip install -r requirements.txt
115-
116-
#. Use ``sphinx-quickstart`` (first-time only!). Do separate source and build. Enter project details through terminal.
117-
118-
Otherwise
119-
120-
#. Fill in index and create other files. Edit conf, like adding mock docs.
121-
#. Build docs with e.g. ``make html``.
122-
#. open ``docs/build/html/index.html`` in browser.
123-
#. host on `ReadTheDocs <https://readthedocs.org/>`__ or GitHub pages.
124-
125-
126-
Some documentation tips
127-
-----------------------
128-
129-
- take a look at our conf.py which we've modified from the generated one
130-
- ex: changing theme to RTD
131-
- intersphinx mapping to connect to other docs
132-
- mock imports
133-
134-
.. code:: bash
135-
136-
# create new environment, press enter to accept
137-
conda create -n docs_env python=3.9
138-
139-
# activate environment
140-
conda activate docs_env
141-
142-
# install requirements
143-
(docs_env) pip install -r docs/requirements.txt
144-
145-
#. Will be using `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation. This requires
146-
147-
.. code:: bash
148-
149-
# build docs
150-
(docs_env) python setup.py build_sphinx
151-
152-
153-
154-
#. Use `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation.
155-
156-
15793
Releasing new version and deploying to PyPi
15894
===========================================
15995

@@ -200,5 +136,4 @@ TODO
200136
- example file with hydra
201137
- manifest file to not include file in package
202138
- GitHub actions for releasing to PyPi when changes to version
203-
- documentation (autodoc)
204139
- adding badges to README

docs/source/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
sys.path.insert(0, os.path.abspath(os.path.join("..", "..")))
2323

24-
MOCK_MODULES = ["matplotlib", "matplotlib.pyplot", "numpy", ""]
24+
MOCK_MODULES = ["matplotlib", "matplotlib.pyplot", "numpy"]
2525
for mod_name in MOCK_MODULES:
2626
sys.modules[mod_name] = mock.Mock()
2727

@@ -77,4 +77,4 @@
7777
# Add any paths that contain custom static files (such as style sheets) here,
7878
# relative to this directory. They are copied after the builtin static files,
7979
# so a file named "default.css" will overwrite the builtin "default.css".
80-
html_static_path = ["_static"]
80+
# html_static_path = ["_static"]

docs/source/documentation.rst

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.

docs/source/fftconvolve.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FFT Convolve
2-
============
1+
Class example
2+
=============
33

44
.. GENERATE DOCUMENTATION FOR ALL CLASSES
55
.. .. automodule:: pydevtips.fftconvolve
@@ -8,7 +8,7 @@ FFT Convolve
88
.. :special-members: __init__, __call__
99
1010
11-
.. GENERATE ONE AT A TIME WITH CUSTOM TEXT
11+
.. GENERATE INDIVIDUALLY WITH CUSTOM TEXT
1212
1313
There are two classes in this module for performing convolution in the frequency domain with a fixed filter.
1414

docs/source/index.rst

+5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
Contents
1414
--------
1515

16+
.. toctree::
17+
18+
documentation
19+
1620
.. toctree::
1721
:caption: Code
1822

1923
fftconvolve
24+
utils
2025

2126

2227
Indices and tables

docs/source/utils.rst

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Function example
2+
================
3+
4+
.. GENERATE DOCUMENTATION FOR ALL FUNCTIONS
5+
.. .. automodule:: pydevtips.utils
6+
7+
8+
.. GENERATE INDIVIDUALLY
9+
.. autofunction:: pydevtips.utils.add
10+

pydevtips/fftconvolve.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RFFTConvolve(FFTConvolveBase):
5050
"""Real FFT convolve."""
5151

5252
def __init__(self, filter, length) -> None:
53-
r"""
53+
"""
5454
Create convolver that uses a real-valued filter.
5555
5656
Parameters
@@ -66,11 +66,11 @@ def __init__(self, filter, length) -> None:
6666
super(RFFTConvolve, self).__init__(filter, length)
6767

6868
def _compute_filter_frequency_response(self):
69-
r"""Compute the filter frequency response."""
69+
"""Compute the filter frequency response."""
7070
return np.fft.rfft(self.filter, n=self.pad_length)
7171

7272
def __call__(self, signal) -> np.ndarray:
73-
r"""
73+
"""
7474
Apply the real-valued filter to the signal, in the frequency domain.
7575
7676
Parameters

pydevtips/utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Utility functions.
3+
"""
4+
5+
6+
def add(a, b):
7+
"""
8+
Add two integers.
9+
10+
Parameters
11+
----------
12+
a : int
13+
First integer.
14+
b : int
15+
Second integer.
16+
17+
Returns
18+
-------
19+
result : int
20+
Sum of inputs.
21+
22+
"""
23+
assert isinstance(a, int)
24+
assert isinstance(b, int)
25+
return a + b

0 commit comments

Comments
 (0)