Skip to content

Commit 320c398

Browse files
committed
update notebook gallery
1 parent a484008 commit 320c398

File tree

18 files changed

+373
-72
lines changed

18 files changed

+373
-72
lines changed

.rename_test_dir/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ To publish the documentation via GitHub pages, you have to:
134134
- create the `gh-pages` branch
135135
- enable GitHub pages on `gh-pages` branch using the `/` (root) directory.
136136

137-
The `doc` action builds the documentation via `make html` and pushes it to the `gh-pages` branch. It also runs `make linkcheck` and `make doctest` to check for missing links and test examples in the documentation.
137+
The `doc` action builds the documentation via `make html` and pushes it to the `gh-pages` branch. It also runs `make linkcheck` and `make doctest` to check for missing links and test examples in the documentation.
138+
139+
#### Examples & Notebook Gallery
140+
141+
The `sphinx_gallery` extension generates a gallery of examples from `.py` files in the `examples` folder and adds them to the documentation. This happens **automatically** when building the documentation.
142+
143+
A similar results can be achieved by running the `convert_notebooks.py` script, which takes `.ipynb` files in the `notebooks` folder, converts them to HTML and adds them to the documentation. This has to be run **manually** before building the documentation.

.rename_test_dir/doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
# report broken links
6565
nitpicky = True
6666
# don't check some links
67-
linkcheck_ignore = ['notebooks/.*']
67+
linkcheck_ignore = ['notebooks_rst/.*', '../notebooks_html/.*', '../notebooks_ipynb/.*']
6868

6969
# include all Python files in example gallery (not just files starting with "plot_" as the default)
7070
sphinx_gallery_conf = {"filename_pattern": ''}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
An example notebook...
2+
======================
3+
4+
* `Example_Notebook.html <../notebooks_html/Example_Notebook.html>`_ (view html)\
5+
* `Example_Notebook.ipynb <../notebooks_ipynb/Example_Notebook.ipynb>`_ (download notebook)
6+
7+
--------------------
8+
9+
.. only:: html
10+
11+
.. raw:: html
12+
13+
<iframe src="../notebooks_html/Example_Notebook.html"
14+
style="width:100%; height:80vh; border:0;"
15+
loading="lazy"
16+
referrerpolicy="no-referrer">
17+
</iframe>
File renamed without changes.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from pathlib import Path
2+
import re
3+
import shutil
4+
5+
import nbformat
6+
from nbconvert import PythonExporter, HTMLExporter
7+
8+
HEADING_RE = re.compile(r'^\s*#{1,6}\s+(.*\S)\s*$')
9+
10+
11+
def first_heading(nb):
12+
for cell in nb.cells:
13+
if cell.cell_type != "markdown":
14+
continue
15+
for line in cell.source.splitlines():
16+
m = HEADING_RE.match(line)
17+
if m:
18+
return m.group(1).strip()
19+
return None
20+
21+
22+
def convert(nb_path, py_out=None, html_out=None):
23+
"""Convert a single notebook to .py and .html"""
24+
nb = nbformat.read(nb_path, as_version=4)
25+
# ---- .py ----
26+
if py_out is not None:
27+
py_code, _ = PythonExporter().from_notebook_node(nb)
28+
py_out.write_text(py_code, encoding="utf-8")
29+
# ---- .html ----
30+
if html_out is not None:
31+
html, _ = HTMLExporter(template_name="lab").from_notebook_node(nb)
32+
html_out.write_text(html, encoding="utf-8")
33+
# ---- title ----
34+
title = first_heading(nb)
35+
if title is None:
36+
title = nb.stem
37+
return title
38+
39+
40+
def create_rst(rst_out, title, stem, html_dir, ipynb_dir):
41+
rst = rf"""{title}
42+
{'=' * len(title)}
43+
44+
* `{stem}.html <../{html_dir}/{stem}.html>`_ (view html)\
45+
* `{stem}.ipynb <../{ipynb_dir}/{stem}.ipynb>`_ (download notebook)
46+
47+
--------------------
48+
49+
.. only:: html
50+
51+
.. raw:: html
52+
53+
<iframe src="../{html_dir}/{stem}.html"
54+
style="width:100%; height:80vh; border:0;"
55+
loading="lazy"
56+
referrerpolicy="no-referrer">
57+
</iframe>"""
58+
rst_out.write_text(rst, encoding="utf-8")
59+
60+
61+
def main():
62+
root_dir = Path(__file__).parent.parent.absolute()
63+
nb_dir = root_dir / "notebooks"
64+
doc_dir = root_dir / "doc"
65+
doc_nb_html_sub_dir = "notebooks_html"
66+
doc_nb_rst_sub_dir = "notebooks_rst"
67+
doc_nb_ipynb_sub_dir = "notebooks_ipynb"
68+
doc_nb_html_dir = doc_dir / "extra" / doc_nb_html_sub_dir
69+
doc_nb_ipynb_dir = doc_dir / "extra" / doc_nb_ipynb_sub_dir
70+
doc_nb_rst_dir = doc_dir / doc_nb_rst_sub_dir
71+
test_nb_dir = root_dir / "tests" / "notebooks"
72+
73+
# converted NBs are written into these directories, make sure they exist
74+
for dir in [doc_nb_html_dir, doc_nb_rst_dir, doc_nb_ipynb_dir, test_nb_dir]:
75+
dir.mkdir(exist_ok=True)
76+
77+
print("Using the following paths:")
78+
print(f" root dir: {root_dir}")
79+
print(f" NB dir: {nb_dir}")
80+
print(f" doc dir: {doc_dir}")
81+
print(f" doc NB HTML dir: {doc_nb_html_dir}")
82+
print(f" doc NB RST dir: {doc_nb_rst_dir}")
83+
print(f" doc NB ipynb dir: {doc_nb_ipynb_dir}")
84+
print(f" test NB dir: {test_nb_dir}")
85+
86+
# read gallery header
87+
with open(doc_nb_rst_dir/"README.rst", 'r') as file:
88+
nb_gallery = file.read()
89+
90+
# iterate through notebooks
91+
print("Start notebook conversion:")
92+
for nb in nb_dir.glob("*.ipynb"):
93+
# copy to doc
94+
shutil.copyfile(nb, doc_nb_ipynb_dir / f"{nb.stem}.ipynb")
95+
# get title and convert to .py and .html
96+
title = convert(
97+
nb_path=nb,
98+
py_out=test_nb_dir / f"{nb.stem}.py",
99+
html_out=doc_nb_html_dir / f"{nb.stem}.html",
100+
)
101+
# create RST file
102+
create_rst(rst_out=doc_nb_rst_dir / f"{nb.stem}.rst",
103+
title=title,
104+
stem=nb.stem,
105+
html_dir=doc_nb_html_sub_dir,
106+
ipynb_dir=doc_nb_ipynb_sub_dir)
107+
# add to gallery
108+
nb_gallery += f"\n * `{title} <{doc_nb_rst_sub_dir}/{nb.stem}.html>`_"
109+
print(f" ✓ {nb.name}")
110+
print("DONE")
111+
112+
# write gallery file
113+
(doc_dir / "notebook_gallery.rst").write_text(nb_gallery, encoding="utf-8")
114+
115+
116+
if __name__ == "__main__":
117+
main()

.rename_test_dir/newpackage/rename.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def main(args):
4949
('PythonTemplatePackage', args.package_name, 'Package name in text', ['./doc/index.rst',
5050
'./README.md',
5151
'./examples/README.rst',
52-
'./notebooks/README.rst']),
52+
'./doc/notebooks_rst/README.rst']),
5353
('pythontemplatepackage', args.python_name, 'Python package name', ['./doc/conf.py',
5454
'./doc/index.rst',
5555
'./doc/api_summary.rst',

.rename_test_dir/tests/test_rename.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def __call__(self, dir, sub):
5555
'index.rst',
5656
'requirements.txt',
5757
'_static',
58-
'_templates']]
58+
'_templates',
59+
'notebooks_rst']]
5960
return []
6061

6162
with tempfile.TemporaryDirectory() as tmp_dir:

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ To publish the documentation via GitHub pages, you have to:
134134
- create the `gh-pages` branch
135135
- enable GitHub pages on `gh-pages` branch using the `/` (root) directory.
136136

137-
The `doc` action builds the documentation via `make html` and pushes it to the `gh-pages` branch. It also runs `make linkcheck` and `make doctest` to check for missing links and test examples in the documentation.
137+
The `doc` action builds the documentation via `make html` and pushes it to the `gh-pages` branch. It also runs `make linkcheck` and `make doctest` to check for missing links and test examples in the documentation.
138+
139+
#### Examples & Notebook Gallery
140+
141+
The `sphinx_gallery` extension generates a gallery of examples from `.py` files in the `examples` folder and adds them to the documentation. This happens **automatically** when building the documentation.
142+
143+
A similar results can be achieved by running the `convert_notebooks.py` script, which takes `.ipynb` files in the `notebooks` folder, converts them to HTML and adds them to the documentation. This has to be run **manually** before building the documentation.

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
# report broken links
6565
nitpicky = True
6666
# don't check some links
67-
linkcheck_ignore = ['notebooks/.*']
67+
linkcheck_ignore = ['notebooks_rst/.*', '../notebooks_html/.*', '../notebooks_ipynb/.*']
6868

6969
# include all Python files in example gallery (not just files starting with "plot_" as the default)
7070
sphinx_gallery_conf = {"filename_pattern": ''}

0 commit comments

Comments
 (0)