Skip to content

Commit c7cbeec

Browse files
authored
INTPYTHON-482 Add API docs (#62)
1 parent 17658bb commit c7cbeec

File tree

20 files changed

+4558
-379
lines changed

20 files changed

+4558
-379
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,23 @@ jobs:
7474
working-directory: ${{ matrix.working-directory }}
7575
secrets: inherit
7676

77+
api-docs:
78+
name: API docs
79+
runs-on: ubuntu-latest
80+
steps:
81+
- uses: actions/checkout@v4
82+
with:
83+
persist-credentials: false
84+
- name: Install uv
85+
uses: astral-sh/setup-uv@v5
86+
with:
87+
enable-cache: true
88+
python-version: 3.9
89+
- run: uv run sphinx-build -T -W -b html docs docs/_build/html
90+
7791
ci_success:
7892
name: "CI Success"
79-
needs: [build, lint, test, pre-commit]
93+
needs: [build, lint, test, pre-commit, api-docs]
8094
if: |
8195
always()
8296
runs-on: ubuntu-latest

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ __pycache__
66
.env
77
.venv*
88
.local_atlas_uri
9+
docs/langchain_mongodb
10+
docs/langgraph_checkpoint_mongodb
11+
docs/index.md

.readthedocs.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: 2
2+
build:
3+
os: ubuntu-22.04
4+
tools:
5+
python: "3.11"
6+
jobs:
7+
create_environment:
8+
- asdf plugin add uv
9+
- asdf install uv latest
10+
- asdf global uv latest
11+
install:
12+
- uv sync
13+
build:
14+
html:
15+
- uv run sphinx-build -T -b html docs $READTHEDOCS_OUTPUT/html
16+
sphinx:
17+
configuration: docs/conf.py

docs/_extensions/gallery_directive.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""A directive to generate a gallery of images from structured data.
2+
3+
Generating a gallery of images that are all the same size is a common
4+
pattern in documentation, and this can be cumbersome if the gallery is
5+
generated programmatically. This directive wraps this particular use-case
6+
in a helper-directive to generate it with a single YAML configuration file.
7+
8+
It currently exists for maintainers of the pydata-sphinx-theme,
9+
but might be abstracted into a standalone package if it proves useful.
10+
"""
11+
12+
from pathlib import Path
13+
from typing import Any, ClassVar, Dict, List
14+
15+
from docutils import nodes
16+
from docutils.parsers.rst import directives
17+
from sphinx.application import Sphinx
18+
from sphinx.util import logging
19+
from sphinx.util.docutils import SphinxDirective
20+
from yaml import safe_load
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
TEMPLATE_GRID = """
26+
`````{{grid}} {columns}
27+
{options}
28+
29+
{content}
30+
31+
`````
32+
"""
33+
34+
GRID_CARD = """
35+
````{{grid-item-card}} {title}
36+
{options}
37+
38+
{content}
39+
````
40+
"""
41+
42+
43+
class GalleryGridDirective(SphinxDirective):
44+
"""A directive to show a gallery of images and links in a Bootstrap grid.
45+
46+
The grid can be generated from a YAML file that contains a list of items, or
47+
from the content of the directive (also formatted in YAML). Use the parameter
48+
"class-card" to add an additional CSS class to all cards. When specifying the grid
49+
items, you can use all parameters from "grid-item-card" directive to customize
50+
individual cards + ["image", "header", "content", "title"].
51+
52+
Danger:
53+
This directive can only be used in the context of a Myst documentation page as
54+
the templates use Markdown flavored formatting.
55+
"""
56+
57+
name = "gallery-grid"
58+
has_content = True
59+
required_arguments = 0
60+
optional_arguments = 1
61+
final_argument_whitespace = True
62+
option_spec: ClassVar[dict[str, Any]] = {
63+
# A class to be added to the resulting container
64+
"grid-columns": directives.unchanged,
65+
"class-container": directives.unchanged,
66+
"class-card": directives.unchanged,
67+
}
68+
69+
def run(self) -> List[nodes.Node]:
70+
"""Create the gallery grid."""
71+
if self.arguments:
72+
# If an argument is given, assume it's a path to a YAML file
73+
# Parse it and load it into the directive content
74+
path_data_rel = Path(self.arguments[0])
75+
path_doc, _ = self.get_source_info()
76+
path_doc = Path(path_doc).parent
77+
path_data = (path_doc / path_data_rel).resolve()
78+
if not path_data.exists():
79+
logger.info(f"Could not find grid data at {path_data}.")
80+
nodes.text("No grid data found at {path_data}.")
81+
return
82+
yaml_string = path_data.read_text()
83+
else:
84+
yaml_string = "\n".join(self.content)
85+
86+
# Use all the element with an img-bottom key as sites to show
87+
# and generate a card item for each of them
88+
grid_items = []
89+
for item in safe_load(yaml_string):
90+
# remove parameters that are not needed for the card options
91+
title = item.pop("title", "")
92+
93+
# build the content of the card using some extra parameters
94+
header = f"{item.pop('header')} \n^^^ \n" if "header" in item else ""
95+
image = f"![image]({item.pop('image')}) \n" if "image" in item else ""
96+
content = f"{item.pop('content')} \n" if "content" in item else ""
97+
98+
# optional parameter that influence all cards
99+
if "class-card" in self.options:
100+
item["class-card"] = self.options["class-card"]
101+
102+
loc_options_str = "\n".join(f":{k}: {v}" for k, v in item.items()) + " \n"
103+
104+
card = GRID_CARD.format(
105+
options=loc_options_str, content=header + image + content, title=title
106+
)
107+
grid_items.append(card)
108+
109+
# Parse the template with Sphinx Design to create an output container
110+
# Prep the options for the template grid
111+
class_ = "gallery-directive" + f' {self.options.get("class-container", "")}'
112+
options = {"gutter": 2, "class-container": class_}
113+
options_str = "\n".join(f":{k}: {v}" for k, v in options.items())
114+
115+
# Create the directive string for the grid
116+
grid_directive = TEMPLATE_GRID.format(
117+
columns=self.options.get("grid-columns", "1 2 3 4"),
118+
options=options_str,
119+
content="\n".join(grid_items),
120+
)
121+
122+
# Parse content as a directive so Sphinx Design processes it
123+
container = nodes.container()
124+
self.state.nested_parse([grid_directive], 0, container)
125+
126+
# Sphinx Design outputs a container too, so just use that
127+
return [container.children[0]]
128+
129+
130+
def setup(app: Sphinx) -> Dict[str, Any]:
131+
"""Add custom configuration to sphinx app.
132+
133+
Args:
134+
app: the Sphinx application
135+
136+
Returns:
137+
the 2 parallel parameters set to ``True``.
138+
"""
139+
app.add_directive("gallery-grid", GalleryGridDirective)
140+
141+
return {
142+
"parallel_read_safe": True,
143+
"parallel_write_safe": True,
144+
}

docs/_static/favicon.png

777 Bytes
Loading

docs/_static/wordmark-api-dark.svg

Lines changed: 11 additions & 0 deletions
Loading

docs/_static/wordmark-api.svg

Lines changed: 11 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)