Skip to content

Commit 75fe45a

Browse files
committed
Add custom css/js, move to myst-nb and add notebook support
1 parent cf2f824 commit 75fe45a

9 files changed

Lines changed: 162 additions & 71 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ docs/_build/
130130
docs/api
131131
docs/index.md
132132
docs/html
133+
docs/jupyter_execute
133134
index.md
134135
_template/labextension
135136

docs/notebooks/example.ipynb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Example Notebook\n",
8+
"This is an example notebook."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 3,
14+
"metadata": {},
15+
"outputs": [
16+
{
17+
"name": "stdout",
18+
"output_type": "stream",
19+
"text": [
20+
"hello world!\n",
21+
"\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"print(\"hello world!\")"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": []
35+
}
36+
],
37+
"metadata": {
38+
"kernelspec": {
39+
"display_name": "Python 3.11 (XPython)",
40+
"language": "python",
41+
"name": "xpython"
42+
},
43+
"language_info": {
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"version": "3.11.2"
48+
}
49+
},
50+
"nbformat": 4,
51+
"nbformat_minor": 2
52+
}

docs/src/configuration.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,31 @@ autodoc_pydantic_model_show_json = true
129129
autodoc_pydantic_settings_show_json = false
130130
autodoc_pydantic_model_show_field_summary = false
131131
```
132+
133+
## Myst-NB
134+
135+
```toml
136+
[tool.yardang]
137+
jupyter_execute_notebooks = "off"
138+
execution_excludepatterns = []
139+
```
140+
141+
Notebooks can be included with:
142+
143+
````raw
144+
```{eval-rst}
145+
.. toctree::
146+
:maxdepth: 1
147+
148+
../notebooks/example
149+
```
150+
````
151+
152+
An example follows:
153+
154+
```{eval-rst}
155+
.. toctree::
156+
:maxdepth: 1
157+
158+
../notebooks/example
159+
```

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ classifiers = [
2727
dependencies = [
2828
"autodoc-pydantic",
2929
"furo",
30-
"myst-parser",
30+
"myst-nb",
3131
"packaging",
3232
"rich",
3333
"sphinx>=7.2.6,<8.1",
@@ -77,7 +77,7 @@ replace = 'version = "{new_version}"'
7777
ignore = [
7878
".copier-answers.yml",
7979
"docs/*",
80-
"docs/src/*",
80+
"docs/**/*",
8181
"Makefile",
8282
"setup.py",
8383
]

yardang/build.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,7 @@
66
from typing import List, Optional
77
from .utils import get_config
88

9-
__all__ = (
10-
"generate_docs_configuration",
11-
"CUSTOM_CSS",
12-
)
13-
14-
# Wider screen for furo
15-
CUSTOM_CSS = """
16-
/* Wide main page */
17-
.content {
18-
flex: 1;
19-
}
20-
aside.sidebar-drawer {
21-
width: unset;
22-
}
23-
24-
/* Left-align tables */
25-
article table.align-default {
26-
margin-left: 0;
27-
}
28-
"""
9+
__all__ = ("generate_docs_configuration",)
2910

3011

3112
@contextmanager
@@ -44,6 +25,8 @@ def generate_docs_configuration(
4425
cname: str = "",
4526
pages: Optional[List] = None,
4627
use_autoapi: Optional[bool] = None,
28+
custom_css: Optional[Path] = None,
29+
custom_js: Optional[Path] = None,
4730
):
4831
if os.path.exists("conf.py"):
4932
# yield folder path to sphinx build
@@ -80,9 +63,15 @@ def generate_docs_configuration(
8063
cname = cname or get_config(section="cname")
8164
pages = pages or get_config(section="pages") or []
8265
use_autoapi = use_autoapi or get_config(section="use-autoapi")
66+
67+
custom_css = custom_css or get_config(section="custom-css") or (Path(__file__).parent / "custom.css")
68+
custom_js = custom_js or get_config(section="custom-js") or (Path(__file__).parent / "custom.js")
69+
8370
source_dir = os.path.curdir
84-
autodoc_pydantic_args = {}
71+
72+
configuration_args = {}
8573
for f in (
74+
# autodoc/autodoc-pydantic
8675
"autodoc_pydantic_model_show_config_summary",
8776
"autodoc_pydantic_model_show_validator_summary",
8877
"autodoc_pydantic_model_show_validator_members",
@@ -92,10 +81,20 @@ def generate_docs_configuration(
9281
"autodoc_pydantic_model_show_json",
9382
"autodoc_pydantic_settings_show_json",
9483
"autodoc_pydantic_model_show_field_summary",
84+
# myst/myst-nb
85+
"jupyter_execute_notebooks",
86+
"execution_excludepatterns",
9587
):
96-
default_value = {"autodoc_pydantic_model_member_order": '"bysource"', "autodoc_pydantic_model_show_json": True}.get(f, False)
88+
default_value = {
89+
# autodoc/autodoc-pydantic
90+
"autodoc_pydantic_model_member_order": '"bysource"',
91+
"autodoc_pydantic_model_show_json": True,
92+
# myst/myst-nb
93+
"execution_excludepatterns": [],
94+
"jupyter_execute_notebooks": "off",
95+
}.get(f, False)
9796
config_value = get_config(section=f"{f}")
98-
autodoc_pydantic_args[f] = default_value if config_value is None else config_value
97+
configuration_args[f] = default_value if config_value is None else config_value
9998
# create a temporary directory to store the conf.py file in
10099
with TemporaryDirectory() as td:
101100
templateEnv = Environment(loader=FileSystemLoader(searchpath=str(Path(__file__).parent.resolve())))
@@ -115,12 +114,18 @@ def generate_docs_configuration(
115114
pages=pages,
116115
use_autoapi=use_autoapi,
117116
source_dir=source_dir,
118-
**autodoc_pydantic_args,
117+
**configuration_args,
119118
)
120119
# dump to file
121120
template_file = Path(td) / "conf.py"
122121
template_file.write_text(template)
123122

123+
# write custom css and customjs
124+
Path("docs/html/_static/styles").mkdir(parents=True, exist_ok=True)
125+
Path("docs/html/_static/styles/custom.css").write_text(custom_css.read_text())
126+
Path("docs/html/_static/js").mkdir(parents=True, exist_ok=True)
127+
Path("docs/html/_static/js/custom.js").write_text(custom_js.read_text())
128+
124129
# append docs-specific ignores to gitignore
125130
if Path(".gitignore").exists():
126131
has_html_build_folder = False

yardang/cli.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
from sys import executable
2-
from pathlib import Path
32
from subprocess import Popen, PIPE
43
from typer import Typer
54

6-
from .build import generate_docs_configuration, CUSTOM_CSS
5+
from .build import generate_docs_configuration
76

87

98
def build(quiet: bool = False, debug: bool = False):
109
with generate_docs_configuration() as file:
11-
folder = Path("docs/html/_static/styles")
12-
css = folder / "custom.css"
13-
if not css.exists():
14-
folder.mkdir(parents=True, exist_ok=True)
15-
css.write_text(CUSTOM_CSS)
16-
1710
build_cmd = [
1811
executable,
1912
"-m",

yardang/conf.py.j2

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use_autoapi = {{use_autoapi}} # noqa: F821
2929
# Standardized below #
3030
######################
3131
extensions = [
32-
"myst_parser",
33-
"sphinx.ext.viewcode",
32+
"myst_nb",
3433
"sphinx.ext.napoleon",
3534
"sphinx_design",
3635
"sphinx_copybutton",
@@ -41,7 +40,19 @@ extensions = [
4140
]
4241
if use_autoapi in (True, None):
4342
# add if it is set to true or if it is set to None
44-
extensions.append("autoapi.extension")
43+
# NOTE: bug in autoapi that requires
44+
# viewcode to come after
45+
extensions.extend([
46+
"autoapi.extension",
47+
"sphinx.ext.viewcode",
48+
])
49+
else:
50+
# NOTE: bug in autoapi that requires
51+
# viewcode to come after
52+
extensions.append([
53+
"sphinx.ext.viewcode",
54+
])
55+
4556

4657
os.environ["SPHINX_BUILDING"] = "1"
4758
html_theme = "{{theme}}"
@@ -50,19 +61,32 @@ html_static_path = []
5061
html_css_files = [
5162
"styles/custom.css",
5263
]
64+
html_js_files = [
65+
"js/custom.js",
66+
]
67+
5368
master_doc = "index"
5469
templates_path = ["_templates"]
5570
source_suffix = [".rst", ".md"]
5671
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "node_modules", "_skbuild", ".pytest_cache", "js/*"]
5772
language = "en"
5873
pygments_style = "sphinx"
74+
75+
# myst / myst-nb
76+
myst_enable_extensions = ["colon_fence"]
77+
jupyter_execute_notebooks = "{{jupyter_execute_notebooks}}"
78+
execution_excludepatterns = {{execution_excludepatterns}}
79+
80+
# autosummary
5981
autosummary_generate = True
82+
83+
# autoapi
6084
autoapi_dirs = [module]
6185
autoapi_python_class_content = "both"
62-
myst_enable_extensions = ["colon_fence"]
63-
autodoc_default_options = {
64-
"show-inheritance": True,
65-
}
86+
autoapi_add_toctree_entry = use_autoapi is True
87+
88+
# autodoc/autodoc-pydantic
89+
autodoc_default_options = {"show-inheritance": True}
6690
autodoc_pydantic_model_show_config_summary = {{autodoc_pydantic_model_show_config_summary}} # noqa: F821
6791
autodoc_pydantic_model_show_validator_summary = {{autodoc_pydantic_model_show_validator_summary}} # noqa: F821
6892
autodoc_pydantic_model_show_validator_members = {{autodoc_pydantic_model_show_validator_members}} # noqa: F821
@@ -71,7 +95,7 @@ autodoc_pydantic_field_show_constraints = {{autodoc_pydantic_field_show_constrai
7195
autodoc_pydantic_model_member_order = {{autodoc_pydantic_model_member_order}} # noqa: F821
7296
autodoc_pydantic_model_show_json = {{autodoc_pydantic_model_show_json}} # noqa: F821
7397
autodoc_pydantic_settings_show_json = {{autodoc_pydantic_settings_show_json}} # noqa: F821
74-
autoapi_add_toctree_entry = use_autoapi is True
98+
7599
toctree_base = """{toctree}
76100
---
77101
caption: ""
@@ -94,36 +118,12 @@ def run_copycname(_):
94118
if cname:
95119
out.write_text(cname)
96120

97-
def run_create_version_marker_to_be_committed(_):
121+
def run_create_previous_version_markdown(_):
98122
versions_folder = Path("{{source_dir}}") / "docs" / "versions"
99123
if not versions_folder.exists():
100124
versions_folder.mkdir(parents=True, exist_ok=True)
101-
version_file = versions_folder / f"{version}.txt"
102-
version_file.write_text("commit this file to ensure these docs can be referenced in the future")
103-
104-
def run_create_older_version_docs(_):
105-
versions_folder = Path("{{source_dir}}") / "docs" / "versions"
106-
if not versions_folder.exists():
107-
# no older versions yet
108-
return
109-
all_versions = [f.replace(".txt", "") for f in os.listdir(str(versions_folder)) if f.endswith(".txt")]
110-
all_versions_as_versions = []
111-
invalid_version = Version("999.999.999")
112-
for version in all_versions:
113-
try:
114-
all_versions_as_versions.append(Version(version))
115-
except BaseException:
116-
all_versions_as_versions.append(invalid_version)
117-
all_versions_as_versions.sort(reverse=True)
118-
out = Path("{{source_dir}}") / "docs" / "versions" / "versions.md"
119-
out.write_text("# Previous Versions\n\n")
120-
for i, older_version in enumerate(all_versions_as_versions):
121-
if older_version != invalid_version and str(older_version) in all_versions:
122-
older_version_literal = str(older_version)
123-
with out.open("a") as outfile:
124-
outfile.write(f"- [{older_version_literal}]({docs_host_root}/{name}/{older_version_literal}/)\n")
125-
with out.open("a") as outfile:
126-
outfile.write("\n")
125+
version_file = versions_folder / "version.md"
126+
version_file.write_text("# Previous Versions")
127127

128128
def run_add_version_links_to_toctree(app, doctree):
129129
from sphinx.addnodes import toctree
@@ -148,8 +148,7 @@ def run_add_version_links_to_toctree(app, doctree):
148148
nodes[-1]["includefiles"].append(toc_entry)
149149

150150
def setup(app):
151-
{# app.connect("builder-inited", run_create_older_version_docs) #}
152-
{# app.connect("builder-inited", run_create_version_marker_to_be_committed) #}
151+
{# app.connect("builder-inited", run_create_previous_version_markdown) #}
153152
app.connect("builder-inited", run_copyreadme)
154153
app.connect("builder-inited", run_copycname)
155154
{# app.connect("doctree-read", run_add_version_links_to_toctree, priority=500) #}

yardang/custom.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Wide main page */
2+
.content {
3+
flex: 1;
4+
}
5+
aside.sidebar-drawer {
6+
width: unset;
7+
}
8+
9+
/* Left-align tables */
10+
article table.align-default {
11+
margin-left: 0;
12+
}

yardang/custom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO

0 commit comments

Comments
 (0)