forked from machow/quartodoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.qmd
140 lines (96 loc) · 2.83 KB
/
README.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
format: gfm
jupyter:
jupytext:
text_representation:
extension: .qmd
format_name: quarto
format_version: '1.0'
jupytext_version: 1.14.1
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---
# quartodoc
Generate python API documentation for quarto.
## Install
```
pip install quartodoc
```
Or for the latest changes:
```
python3 -m pip install -e git+https://github.com/machow/quartodoc.git#egg=quartodoc
```
## Basic use
### Render a docstring
```{python}
#| output: false
from quartodoc import get_function, MdRenderer
# get function object ---
f_obj = get_function("quartodoc", "get_function")
# render ---
renderer = MdRenderer(header_level = 1)
print(
renderer.to_md(f_obj)
)
```
# get_function
`get_function(module: str, func_name: str, parser: str = 'numpy')`
Fetch a function.
## Parameters
| Name | Type | Description | Default |
|-------------|--------|----------------------------|-----------|
| `module` | str | A module name. | required |
| `func_name` | str | A function name. | required |
| `parser` | str | A docstring parser to use. | `'numpy'` |
## Examples
```python
>>> get_function("quartodoc", "get_function")
<Function('get_function', ...
```
### Build an index page
```{python}
#| output: false
from quartodoc.autosummary import BuilderPkgdown
builder = BuilderPkgdown(
[
{"title": "first", "desc": "section 1", "contents": ["get_object", "get_function"]},
{"title": "2nd", "desc": "section 2", "contents": ["MdRenderer"]}
],
"quartodoc"
)
print(
builder.render_index()
)
```
## How it works
quartodoc consists of two pieces:
* **collection**: using the library [griffe](https://github.com/mkdocstrings/griffe) to statically
collect information about functions and classes in a program.
* **docstring parsing**: also handled by griffe, which breaks it into a tree structure.
* **docstring rendering**: use plum-dispatch on methods like MdRenderer.to_md to decide
how to visit and render each piece of the tree (e.g. the examples section, a parameter, etc..).
Here is a quick example of how you can grab a function from griffe and walk through it.
```{python}
from griffe.loader import GriffeLoader
from griffe.docstrings.parsers import Parser
griffe = GriffeLoader(docstring_parser = Parser("numpy"))
mod = griffe.load_module("quartodoc")
f_obj = mod._modules_collection["quartodoc.get_function"]
```
```{python}
f_obj.name
```
```{python}
docstring = f_obj.docstring.parsed
docstring
```
Note that quartodoc's MdRenderer can be called on any part of the parsed docstring.
```{python}
from quartodoc import MdRenderer
renderer = MdRenderer()
print(
renderer.to_md(docstring[1])
)
```