|
| 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 | + } |
0 commit comments