You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/42-software-reuse.md
+73-14Lines changed: 73 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -274,23 +274,29 @@ See [Matias Singer's curated list of awesome READMEs](https://github.com/matiass
274
274
275
275
[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.
276
276
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:
278
279
279
280
```bash
280
281
pip install mkdocs mkdocstrings[python]
281
282
```
282
283
284
+
After installation, you can intialize a new MKDocs project in our Python project:
285
+
283
286
```bash
284
287
mkdocs new .
285
288
```
286
289
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
+
287
297
```yaml
288
298
site_name: Inflam
289
299
290
-
theme:
291
-
name: "mkdocs"
292
-
font: false
293
-
294
300
nav:
295
301
- Overview: index.md
296
302
@@ -299,17 +305,50 @@ plugins:
299
305
- mkdocstrings
300
306
```
301
307
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
+
```
302
315
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.
304
319
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.
305
321
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.
307
324
325
+
::::::::::::::::::::::::::::::::: challenge
308
326
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
+
```
310
344
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.
311
346
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:
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:
331
391
332
392
```bash
333
393
git add inflammation/models.py mkdocs.yml docs/
334
394
```
335
395
336
-
Deploy the documentation to GitHub Pages:
337
-
396
+
To deploy the documentation to GitHub Pages, you can use the following command:
338
397
```bash
339
398
mkdocs gh-deploy
340
399
```
341
400
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.
343
402
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.
0 commit comments