Skip to content

Commit 50403ee

Browse files
Merge pull request #2903 from jessica-mitchell/cleanup-conf
Re-add Jupyter notebook download to examples and move script to _ext folder
2 parents 445db5a + 6b0d96e commit 50403ee

3 files changed

Lines changed: 119 additions & 86 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# add_button_notebook.py
4+
#
5+
# This file is part of NEST.
6+
#
7+
# Copyright (C) 2004 The NEST Initiative
8+
#
9+
# NEST is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 2 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# NEST is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
21+
22+
import glob
23+
import os
24+
import sys
25+
from sphinx.application import Sphinx
26+
from pathlib import Path
27+
28+
29+
def add_button_to_examples(app, env, docnames):
30+
"""Find all examples and include a link to launch notebook.
31+
32+
Function finds all restructured text files in auto_examples
33+
and injects the multistring prolog, which is rendered
34+
as a button link in HTML. The target is set to a Jupyter notebook of
35+
the same name and a service to run it.
36+
The nameholder in the string is replaced with the file name.
37+
38+
The rst files are generated at build time by Sphinx_gallery.
39+
The notebooks that the target points to are linked with
40+
services (like EBRAINS JupyterHub) that runs notebooks using nbgitpuller.
41+
See https://hub.jupyter.org/nbgitpuller/link.html
42+
The notebooks are located in the repository nest/nest-simulator-examples/.
43+
The notebooks are generated from the CI workflow of NEST
44+
on GitHub, which converts the source Python files to .ipynb.
45+
46+
The link to run the notebook is rendered in an image within a card directive.
47+
"""
48+
example_prolog = """
49+
.. only:: html
50+
51+
----
52+
53+
Run this example as a Jupyter notebook:
54+
55+
.. card::
56+
:width: 25%
57+
:margin: 2
58+
:text-align: center
59+
:link: https://lab.ebrains.eu/hub/user-redirect/\
60+
git-pull?repo=https%3A%2F%2Fgithub.com%2Fnest%2Fnest-simulator-examples&urlpath=lab\
61+
%2Ftree%2Fnest-simulator-examples%2Fnotebooks%2Fnotebooks%2Ffilepath.ipynb&branch=main
62+
:link-alt: JupyterHub service
63+
64+
.. image:: https://nest-simulator.org/TryItOnEBRAINS.png
65+
66+
67+
.. grid:: 1 1 1 1
68+
:padding: 0 0 2 0
69+
70+
.. grid-item::
71+
:class: sd-text-muted
72+
:margin: 0 0 3 0
73+
:padding: 0 0 3 0
74+
:columns: 4
75+
76+
See :ref:`our guide <run_jupyter>` for more information and troubleshooting.
77+
78+
----
79+
"""
80+
# Find all relevant files
81+
# Inject prolog into Python example
82+
files = list(Path("auto_examples/").rglob("*.rst"))
83+
for file in files:
84+
# Skip index files and benchmark file. These files do not have notebooks that can run
85+
# on the service.
86+
if file.stem == "index" or file.stem == "hpc_benchmark":
87+
continue
88+
89+
with open(file, "r") as f:
90+
parent = Path("auto_examples/")
91+
path2example = os.path.relpath(file, parent)
92+
path2example = os.path.splitext(path2example)[0]
93+
path2example = path2example.replace("/", "%2F")
94+
prolog = example_prolog.replace("filepath", path2example)
95+
96+
lines = f.readlines()
97+
98+
# find the first heading of the file.
99+
for i, item in enumerate(lines):
100+
if item.startswith("-----"):
101+
break
102+
103+
# insert prolog into rst file after heading
104+
lines.insert(i + 1, prolog + "\n")
105+
106+
with open(file, "w") as f:
107+
lines = "".join(lines)
108+
f.write(lines)
109+
110+
111+
def setup(app):
112+
app.connect("env-before-read-docs", add_button_to_examples)
113+
114+
return {
115+
"version": "0.1",
116+
"parallel_read_safe": True,
117+
"parallel_write_safe": True,
118+
}

doc/htmldoc/conf.py

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"sphinx.ext.doctest",
5555
"sphinx.ext.intersphinx",
5656
"sphinx.ext.mathjax",
57+
"add_button_notebook",
5758
"IPython.sphinxext.ipython_console_highlighting",
5859
"nbsphinx",
5960
"sphinx_design",
@@ -211,88 +212,6 @@ def get_pynest_list(app, env, docname):
211212
ExtractPyNESTAPIS()
212213

213214

214-
def add_button_to_examples(app, env, docnames):
215-
"""Find all examples and include a link to launch notebook.
216-
217-
Function finds all restructured text files in auto_examples
218-
and injects the multistring prolog, which is rendered
219-
as a button link in HTML. The target is set to a Jupyter notebook of
220-
the same name and a service to run it.
221-
The nameholder in the string is replaced with the file name.
222-
223-
The rst files are generated at build time by Sphinx_gallery.
224-
The notebooks that the target points to are linked with
225-
services (like EBRAINS JupyterHub) that runs notebooks using nbgitpuller.
226-
See https://hub.jupyter.org/nbgitpuller/link.html
227-
The notebooks are located in the repository nest/nest-simulator-examples/.
228-
The notebooks are generated from the CI workflow of NEST
229-
on GitHub, which converts the source Python files to .ipynb.
230-
231-
The link to run the notebook is rendered in an image within a card directive.
232-
"""
233-
example_prolog = """
234-
.. only:: html
235-
236-
----
237-
238-
Run this example as a Jupyter notebook:
239-
240-
.. card::
241-
:width: 25%
242-
:margin: 2
243-
:text-align: center
244-
:link: https://lab.ebrains.eu/hub/user-redirect/\
245-
git-pull?repo=https%3A%2F%2Fgithub.com%2Fnest%2Fnest-simulator-examples&urlpath=lab\
246-
%2Ftree%2Fnest-simulator-examples%2Fnotebooks%2Fnotebooks%2Ffilepath.ipynb&branch=main
247-
:link-alt: JupyterHub service
248-
249-
.. image:: https://nest-simulator.org/TryItOnEBRAINS.png
250-
251-
252-
.. grid:: 1 1 1 1
253-
:padding: 0 0 2 0
254-
255-
.. grid-item::
256-
:class: sd-text-muted
257-
:margin: 0 0 3 0
258-
:padding: 0 0 3 0
259-
:columns: 4
260-
261-
See :ref:`our guide <run_jupyter>` for more information and troubleshooting.
262-
263-
----
264-
"""
265-
# Find all relevant files
266-
# Inject prolog into Python example
267-
files = list(Path("auto_examples/").rglob("*.rst"))
268-
for file in files:
269-
# Skip index files and benchmark file. These files do not have notebooks that can run
270-
# on the service.
271-
if file.stem == "index" or file.stem == "hpc_benchmark":
272-
continue
273-
274-
with open(file, "r") as f:
275-
parent = Path("auto_examples/")
276-
path2example = os.path.relpath(file, parent)
277-
path2example = os.path.splitext(path2example)[0]
278-
path2example = path2example.replace("/", "%2F")
279-
prolog = example_prolog.replace("filepath", path2example)
280-
281-
lines = f.readlines()
282-
283-
# find the first heading of the file.
284-
for i, item in enumerate(lines):
285-
if item.startswith("-----"):
286-
break
287-
288-
# insert prolog into rst file after heading
289-
lines.insert(i + 1, prolog + "\n")
290-
291-
with open(file, "w") as f:
292-
lines = "".join(lines)
293-
f.write(lines)
294-
295-
296215
def api_customizer(app, docname, source):
297216
if docname == "ref_material/pynest_api/index":
298217
list_apis = json.load(open("api_function_list.json"))
@@ -320,7 +239,6 @@ def setup(app):
320239
app.add_css_file("css/pygments.css")
321240
app.add_js_file("js/custom.js")
322241
app.connect("env-before-read-docs", get_pynest_list)
323-
app.connect("env-before-read-docs", add_button_to_examples)
324242
app.connect("config-inited", config_inited_handler)
325243

326244

doc/htmldoc/static/css/custom.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ section#kernel-attributes-nest-nestmodule dd {
9090
display: none;
9191
}
9292

93-
.sphx-glr-download-jupyter {
94-
display: none;
95-
}
9693

9794
.autosummary tr {
9895
border: none;

0 commit comments

Comments
 (0)