Skip to content

Commit d8ffb62

Browse files
authored
[API Documentation] Cover more submodules (#1179)
* create an optional dependency group for wk-libs examples * remove nifti script to avoid sklearn dependency * fix optional marker * added more submodules to api docs * remove unused imports
1 parent 90386e7 commit d8ffb62

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

docs/generate_api_doc_pages.py

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
1-
import shutil
21
import inspect
3-
import sys
42
from logging import getLogger
53
from pathlib import Path
64

75
import webknossos
86

9-
'''
7+
"""
108
This script generates a mapping of all classes in webknossos to their
119
corresponding MkDocs pages. It is used to generate the API reference.
12-
'''
10+
"""
1311

1412
logger = getLogger(__name__)
1513

1614
OUT_PATH = Path("src/api")
1715

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}
16+
# Initialize a dictionary to store submodules and their classes
17+
submodules_classes = {}
18+
19+
# Iterate through all the members of the module
20+
for name, member in inspect.getmembers(webknossos):
21+
# Check if the member is a module (submodule)
22+
if inspect.ismodule(member) and member.__name__.startswith("webknossos"):
23+
submodule_name = member.__name__
24+
25+
# List classes in the submodule
26+
classes = inspect.getmembers(member, inspect.isclass)
27+
28+
# Filter classes that are defined in this specific submodule
29+
defined_classes = [
30+
cls for cls_name, cls in classes if cls.__module__ == submodule_name
31+
]
32+
33+
# If there are classes defined in this submodule, add them to the dictionary
34+
if defined_classes:
35+
submodules_classes[submodule_name] = defined_classes
36+
37+
# Print the submodules and their classes
38+
for submodule, classes in submodules_classes.items():
39+
# The file content uses a special syntax for MkDocs to render the
40+
# docstrings as Markdown. The syntax is:
41+
# ::: module.submodule.class
42+
# See https://mkdocstrings.github.io/python/
43+
classes_string = "\n".join(
44+
[
45+
f" - {c.__name__}"
46+
for c in classes
47+
if not c.__name__.startswith("_")
48+
]
49+
)
50+
file_content = f"""::: {submodule}
4351
options:
4452
members:\n{classes_string}\n"""
45-
46-
out_path=OUT_PATH.joinpath(module_path)
47-
out_path.mkdir(exist_ok=True, parents=True)
4853

49-
logger.debug(f"Writing API docs to {out_path.joinpath(file_name)}")
50-
out_path.joinpath(file_name).write_text(file_content)
54+
submodule_parts = submodule.split(".")
55+
submodule_name = submodule_parts[-1]
56+
file_name = Path(f"{submodule_name}.md")
57+
file_path = "/".join(submodule_parts[:-1])
58+
59+
out_path = OUT_PATH.joinpath(file_path)
60+
out_path.mkdir(exist_ok=True, parents=True)
61+
62+
logger.debug(f"Writing API docs to {out_path.joinpath(file_name)}")
63+
out_path.joinpath(file_name).write_text(file_content)

docs/mkdocs.yml

Lines changed: 7 additions & 7 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/boundingbox.md
119-
- NDBoundingBox: api/webknossos/geometry/ndboundingbox.md
118+
- BoundingBox: api/webknossos/geometry/bounding_box.md
119+
- NDBoundingBox: api/webknossos/geometry/nd_bounding_box.md
120120
- Mag: api/webknossos/geometry/mag.md
121-
- Vec3Int: api/webknossos/geometry/vec3int.md
122-
- VecInt: api/webknossos/geometry/vecint.md
121+
- Vec3Int: api/webknossos/geometry/vec3_int.md
122+
- VecInt: api/webknossos/geometry/vec_int.md
123123
- Dataset:
124124
- Dataset: api/webknossos/dataset/dataset.md
125125
- Layer: api/webknossos/dataset/layer.md
126-
- MagView: api/webknossos/dataset/magview.md
126+
- MagView: api/webknossos/dataset/mag_view.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/webknossos_context.md
134+
- Authentication & Server Context: api/webknossos/client/context.md
135135
- Administration:
136136
- User: api/webknossos/administration/user.md
137137
- Project: api/webknossos/administration/project.md
@@ -174,7 +174,7 @@ plugins:
174174
show_source: false
175175
docstring_style: null
176176
docstring_section_style: list
177-
heading_level: 2
177+
heading_level: 1
178178
inherited_members: true
179179
merge_init_into_class: true
180180
parameter_headings: true

0 commit comments

Comments
 (0)