Skip to content

Commit a1c3a48

Browse files
committed
mkdocs descriptions
1 parent 23b7496 commit a1c3a48

1 file changed

Lines changed: 73 additions & 14 deletions

File tree

episodes/42-software-reuse.md

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,29 @@ See [Matias Singer's curated list of awesome READMEs](https://github.com/matiass
274274

275275
[MKDocs](https://www.mkdocs.org/) generate project documentation as a static website from Markdown files. The website can then be hosted on GitHub Pages or other static site hosting services, providing a user-friendly interface for accessing the documentation.
276276

277-
We can install MKDocs as a Python package using pip:
277+
We can install MKDocs package using `pip`. Here we also install a plugin `mkdocstrings`, which will be used later.
278+
We advice you to do this within a virtual environment you created before:
278279

279280
```bash
280281
pip install mkdocs mkdocstrings[python]
281282
```
282283

284+
After installation, you can intialize a new MKDocs project in our Python project:
285+
283286
```bash
284287
mkdocs new .
285288
```
286289

290+
This will create the two files in your project: `mkdocs.yml` and `docs/index.md`. The first file `mkdocs.yml` is the configuration file for your documentation site. It serves as the central configuration hub for your MKDocs documentation. It tells MKDocs how to structure your documentation site, which plugins and themes to use,
291+
how to organize navigation, etc.
292+
293+
`docs/index.md` is the main page of your documentation. It is usually the landing page of your documentation site.
294+
295+
Let's first look at the `mkdocs.yml` file. It is almost empty now. We can edit it with the following basic configurations:
296+
287297
```yaml
288298
site_name: Inflam
289299

290-
theme:
291-
name: "mkdocs"
292-
font: false
293-
294300
nav:
295301
- Overview: index.md
296302

@@ -299,17 +305,50 @@ plugins:
299305
- mkdocstrings
300306
```
301307
308+
Here we give a name to our documentation site, `Inflam`. We set up the navigation menu with one item `Overview` that links to `index.md`. We also enable two plugins, `search` to provide search functionality in the documentation site, and `mkdocstrings` to automatically generate API reference documentation from Python docstrings, which we will see later.
309+
310+
We can try to render the documentation site locally and see how it looks like:
311+
312+
```bash
313+
mkdocs serve
314+
```
302315

303-
Update `docs/index.md` with relevant content
316+
This will start to build a local static documentation site and serve it at a local web server.
317+
By default, it will be available at `http://127.0.0.1:8000/`, which will also show in the terminal output.
318+
You can open this URL in your web browser to view the documentation site.
304319

320+
The documentation site now consists some default content about MKDocs. It is rendered from the `docs/index.md` file. Let's edit this file to add some relevant content about our project.
305321

306-
Create `docs/user_guide.md` and add an example function usage guide.
322+
Update `docs/index.md` with some relevant content.
323+
For simplicity, we can borrow the content from our `README.md` file.
307324

325+
::::::::::::::::::::::::::::::::: challenge
308326

309-
Create `docs/API.md` with the following content:
327+
Modify `docs/index.md` with the same content as your `README.md` file.
328+
Render the documentation site locally again with `mkdocs serve`.
329+
Check how it looks like in your web browser.
330+
331+
:::::::::::::::::::::::::::::::::
332+
333+
You can also add more pages to your documentation site by creating more Markdown files in the `docs/` directory, and update the `nav` section in `mkdocs.yml` to include these new pages. For example, we can create a new page for API (Application Programming Interface) reference documentation.
334+
335+
An API reference documents the functions, classes, and methods provided by your software, along with their parameters, return values, and usage examples. This is particularly useful for understanding how to interact with your code programmatically. With `mkdocs` and `mkdocstrings` plugin, we can automatically generate API reference documentation from the docstrings in our Python code.
336+
337+
Let's first create `docs/API.md` with the following content:
338+
339+
```markdown
340+
# API Reference
341+
342+
:::inflammation.models
343+
```
310344

345+
Apart from the title, there is only one line `:::inflammation.models` in this file. This is a special syntax provided by the `mkdocstrings` plugin to indicate that we want to generate API documentation for the `inflammation.models` module. The plugin will parse the docstrings in this module and generate the corresponding documentation.
311346

312-
Update the doctring of `load_csv`, use `numpy` style docstring as below:
347+
Now we can call `mkdocs serve` again to render the documentation site locally and check how the API reference page looks like.
348+
349+
Now we can see all the functions defined in the `inflammation.models` module are automatically documented with their docstrings.
350+
351+
One can make the rendered API documentation more informative by improving the docstrings in the code. For example, we can improve the docstring of the `load_csv` function by following the `numpy` style docstring format. Let's update the doctring of `load_csv` as below:
313352

314353
```python
315354
def load_csv(filename: str) -> np.ndarray:
@@ -327,21 +366,41 @@ def load_csv(filename: str) -> np.ndarray:
327366
"""
328367
```
329368

330-
Add and commit the changes to git:
369+
And also configure `mkdocs.yml` to use `numpy` style docstring format for `mkdocstrings` plugin:
370+
371+
```yaml
372+
site_name: Inflam
373+
374+
nav:
375+
- Overview: index.md
376+
- API Reference: api.md
377+
378+
plugins:
379+
- search
380+
- mkdocstrings:
381+
handlers:
382+
python:
383+
options:
384+
docstring_style: numpy
385+
```
386+
387+
Then we can render the documentation site locally again with `mkdocs serve`, the input parameters and return values of the `load_csv` function are now nicely formatted in a table.
388+
389+
390+
Once you are happy with the documentation site, you can deploy it to GitHub Pages so that others can access it online. First, let's commit the changes we made to the repository:
331391

332392
```bash
333393
git add inflammation/models.py mkdocs.yml docs/
334394
```
335395

336-
Deploy the documentation to GitHub Pages:
337-
396+
To deploy the documentation to GitHub Pages, you can use the following command:
338397
```bash
339398
mkdocs gh-deploy
340399
```
341400

342-
Now go check your repository's GitHub Pages "Settings -> Pages", you should see your the link to your documentation site, which should be like: `https://<github_user_id>.github.io/python-intermediate-inflammation/`
401+
This command assumes you have access to the GitHub repository of the current project. It will automatically create a new branch called `gh-pages` in your repository, which will contain the static files of your documentation site, and push this branch to GitHub.
343402

344-
You can add this link to your GitHub repository landing page description.
403+
Now go check your repository's GitHub Pages "Settings -> Pages", you should see the link to your documentation site, which should be like: `https://<github_user_id>.github.io/python-intermediate-inflammation/`. You can add this link to your GitHub repository landing page description.
345404

346405
### Other Documentation
347406

0 commit comments

Comments
 (0)