Skip to content

Commit 90386e7

Browse files
Replace Pdocs with Mkdocstrings (#1173)
* create an optional dependency group for wk-libs examples * remove nifti script to avoid sklearn dependency * fix optional marker * Replace pdocs with mkdocstrings * remove pdoc templates * only create api docs for classes in wk-libs submodules * fix generate.sh script * added some comments * formatting * update CI for building docs * fix markdown links, changed due mkdocs * Update docs/generate.sh Co-authored-by: Mark Bader <[email protected]> * fix API overview page * restructure navigation menu * Revert "restructure navigation menu" This reverts commit d8e039d. --------- Co-authored-by: markbader <[email protected]>
1 parent 49cb8fa commit 90386e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1868
-1760
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ jobs:
295295
296296
- name: Build Docs
297297
run: |
298-
docs/generate.sh --persist
298+
cd docs
299+
./generate.sh --persist
299300
300301
- name: Push docs (for branch)
301302
if: github.ref_type == 'branch'

.github/workflows/publish_docs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ jobs:
2727
2828
- name: Build Docs
2929
run: |
30-
docs/generate.sh --persist
30+
cd docs
31+
./generate.sh --persist
3132
3233
- name: Push docs
3334
env:

docs/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ dmypy.json
137137
# Cython debug symbols
138138
cython_debug/
139139

140-
/src/api
140+
/src/api/webknossos
141141
/wk-repo
142142
/out

docs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Documentation
22

3-
Run `docs/generate.sh` to open a server rendering the documentation.
4-
To update docstrings restart the server, manually written pages in `src` are auto-reloaded.
3+
## Development
4+
Run `./generate.sh` to open a live-reloading server rendering the documentation.
55

6-
To get a live-reloading server for the docstrings, run `docs/generate.sh --api`. This opens pdoc, which looks differently than the final result, but the actual contents are the same.
6+
## Production
77

8-
To produce the html in `out`, run `docs/generate.sh --persist`.
8+
Run `./generate.sh --persist` to produce the production website/HTML in `out`. Use GitHub Actions for building and deploying the website.
99

1010

1111
## Further links
1212

1313
* https://www.mkdocs.org
14-
* https://pdoc.dev
1514
* https://squidfunk.github.io/mkdocs-material
1615
* https://facelessuser.github.io/pymdown-extensions
1716
* https://python-markdown.github.io/extensions/#officially-supported-extensions
17+
* https://mkdocstrings.github.io/

docs/generate.sh

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#! /usr/bin/env bash
22
set -Eeo pipefail
33

4-
PROJECT_DIR="$(dirname "$(dirname "$0")")"
5-
6-
7-
cd "$PROJECT_DIR/docs"
8-
poetry install
4+
poetry install --no-root
95

106
if [ ! -d "wk-repo" ]; then
117
echo
@@ -14,25 +10,11 @@ if [ ! -d "wk-repo" ]; then
1410
echo 'git clone --depth 1 [email protected]:scalableminds/webknossos.git docs/wk-repo'
1511
exit 1
1612
fi
13+
rm -rf src/api/webknossos
14+
PYTHONPATH=$PYTHONPATH poetry run python generate_api_doc_pages.py
1715

18-
export PDOC_CLASS_MODULES="$(poetry run python get_keyword_mapping.py)"
19-
if [ $# -eq 1 ] && [ "$1" = "--api" ]; then
20-
poetry run pdoc ../webknossos/webknossos !webknossos.dataset._utils -t pdoc_templates/pure_pdoc -h 0.0.0.0 -p 8196
16+
if [ $# -eq 1 ] && [ "$1" = "--persist" ]; then
17+
PYTHONPATH=$PYTHONPATH poetry run mkdocs build
2118
else
22-
rm -rf src/api
23-
poetry run pdoc ../webknossos/webknossos !webknossos.dataset._utils -t pdoc_templates/with_mkdocs -o src/api
24-
# rename .html files to .md
25-
find src/api -iname "*.html" -exec sh -c 'mv "$0" "${0%.html}.md"' {} \;
26-
# assert that API docs are written
27-
webknossos_files="$(find src/api/webknossos -type f -name "*.md" | wc -l)"
28-
if ! [ "$webknossos_files" -gt "50" ]; then
29-
echo "Error: There are too few ($webknossos_files, expected > 80) files in src/api/webknossos,"
30-
echo "probably there was an error with pdoc before!"
31-
exit 1
32-
fi
33-
if [ $# -eq 1 ] && [ "$1" = "--persist" ]; then
34-
PYTHONPATH=$PYTHONPATH:. poetry run mkdocs build
35-
else
36-
PYTHONPATH=$PYTHONPATH:. poetry run mkdocs serve -a localhost:8197 --watch-theme
37-
fi
19+
PYTHONPATH=$PYTHONPATH poetry run mkdocs serve -a localhost:8197 --watch-theme
3820
fi

docs/generate_api_doc_pages.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import shutil
2+
import inspect
3+
import sys
4+
from logging import getLogger
5+
from pathlib import Path
6+
7+
import webknossos
8+
9+
'''
10+
This script generates a mapping of all classes in webknossos to their
11+
corresponding MkDocs pages. It is used to generate the API reference.
12+
'''
13+
14+
logger = getLogger(__name__)
15+
16+
OUT_PATH = Path("src/api")
17+
18+
for key, value in webknossos.__dict__.items():
19+
if getattr(value, "__module__", "").startswith("webknossos"):
20+
21+
logger.debug("Processing module", key)
22+
23+
module = value.__module__
24+
25+
module_parts = module.split('.')
26+
module_name = module_parts[-1]
27+
module_path = "/".join(module_parts[:-1])
28+
file_name = f"{key.lower()}.md"
29+
30+
# Extract all classes from the module
31+
classes = inspect.getmembers(sys.modules[module], inspect.isclass)
32+
33+
# Only include classes that are in implemented in that module
34+
classes = [c for c in classes if c[1].__module__ == module]
35+
classes = [c for c in classes if not c[0].startswith("_")]
36+
37+
# The file content uses a special syntax for MkDocs to render the
38+
# docstrings as Markdown. The syntax is:
39+
# ::: module.submodule.class
40+
# See https://mkdocstrings.github.io/python/
41+
classes_string = "\n".join([f" - {c[0]}" for c in classes])
42+
file_content = f"""::: {module}
43+
options:
44+
members:\n{classes_string}\n"""
45+
46+
out_path=OUT_PATH.joinpath(module_path)
47+
out_path.mkdir(exist_ok=True, parents=True)
48+
49+
logger.debug(f"Writing API docs to {out_path.joinpath(file_name)}")
50+
out_path.joinpath(file_name).write_text(file_content)

docs/get_keyword_mapping.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

docs/md_extensions/pdoc_toc_extension.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

docs/mkdocs.yml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ nav:
115115
- API Reference:
116116
- Overview: api/webknossos.md
117117
- Geometry:
118-
- BoundingBox: api/webknossos/geometry/bounding_box.md
119-
- NDBoundingBox: api/webknossos/geometry/nd_bounding_box.md
118+
- BoundingBox: api/webknossos/geometry/boundingbox.md
119+
- NDBoundingBox: api/webknossos/geometry/ndboundingbox.md
120120
- Mag: api/webknossos/geometry/mag.md
121-
- Vec3Int: api/webknossos/geometry/vec3_int.md
122-
- VecInt: api/webknossos/geometry/vec_int.md
121+
- Vec3Int: api/webknossos/geometry/vec3int.md
122+
- VecInt: api/webknossos/geometry/vecint.md
123123
- Dataset:
124124
- Dataset: api/webknossos/dataset/dataset.md
125125
- Layer: api/webknossos/dataset/layer.md
126-
- MagView: api/webknossos/dataset/mag_view.md
126+
- MagView: api/webknossos/dataset/magview.md
127127
- View: api/webknossos/dataset/view.md
128128
- Annotation: api/webknossos/annotation/annotation.md
129129
- Skeleton:
130130
- Skeleton: api/webknossos/skeleton/skeleton.md
131131
- Group: api/webknossos/skeleton/group.md
132132
- Tree: api/webknossos/skeleton/tree.md
133133
- Node: api/webknossos/skeleton/node.md
134-
- Authentication & Server Context: api/webknossos/client/context.md
134+
- Authentication & Server Context: api/webknossos/client/webknossos_context.md
135135
- Administration:
136136
- User: api/webknossos/administration/user.md
137137
- Project: api/webknossos/administration/project.md
@@ -153,6 +153,9 @@ nav:
153153
- Blog: https://medium.com/scalableminds" target="_blank
154154

155155
plugins:
156+
- gen-files:
157+
scripts:
158+
- generate_api_doc_pages.py
156159
- search
157160
- redirects:
158161
redirect_maps:
@@ -163,18 +166,47 @@ plugins:
163166
is_video: False
164167
mark: "youtube-video"
165168
- glightbox
169+
- mkdocstrings:
170+
default_handler: python
171+
handlers:
172+
python:
173+
options:
174+
show_source: false
175+
docstring_style: null
176+
docstring_section_style: list
177+
heading_level: 2
178+
inherited_members: true
179+
merge_init_into_class: true
180+
parameter_headings: true
181+
preload_modules: [webknossos]
182+
separate_signature: true
183+
show_root_heading: true
184+
show_root_full_path: true
185+
show_signature_annotations: true
186+
show_symbol_type_heading: true
187+
show_symbol_type_toc: true
188+
show_if_no_docstring: true
189+
signature_crossrefs: true
190+
summary: true
191+
unwrap_annotated: true
192+
line_length: 60
193+
filters:
194+
- "!^_" # ignore class members with leading underscore
195+
- "!^__" # ignore (private) class members with leading double underscore
196+
- "!RE$" # ignore REGEX variables ending with ...RE
197+
- "!logger" # ignore class-level loggers
166198

167199
markdown_extensions:
168200
- admonition
169201
- attr_list
170202
- md_in_html
171-
- md_extensions.pdoc_toc_extension
203+
- footnotes
172204
- pymdownx.highlight
173205
- pymdownx.inlinehilite
174206
- pymdownx.superfences
175207
- pymdownx.snippets:
176208
base_path: ".."
177209
check_paths: true
178210
- pymdownx.emoji:
179-
emoji_index: !!python/name:materialx.emoji.twemoji
180-
emoji_generator: !!python/name:materialx.emoji.to_svg
211+
emoji_index: !!python/name:material.extensions.emoji.twemoji
212+
emoji_generator: !!python/name:material.extensions.emoji.to_svg

0 commit comments

Comments
 (0)