Skip to content

Commit cf27f51

Browse files
Merge branch 'develop' into pr-625
2 parents df2d9d6 + 618e4db commit cf27f51

File tree

253 files changed

+14322
-11293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

253 files changed

+14322
-11293
lines changed

.dockerignore

Lines changed: 0 additions & 146 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Convert Python example scripts to Jupyter notebooks for Colab integration.
3+
4+
This script converts Sphinx-Gallery style Python examples to Jupyter notebooks.
5+
It is adapted from Braindecode's docs workflow utilities.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import argparse
11+
import copy
12+
from pathlib import Path
13+
14+
import nbformat
15+
from sphinx_gallery import gen_gallery
16+
from sphinx_gallery.notebook import jupyter_notebook, save_notebook
17+
from sphinx_gallery.py_source_parser import split_code_and_text_blocks
18+
19+
20+
def convert_script_to_notebook(
21+
src_file: Path, output_file: Path, gallery_conf: dict
22+
) -> None:
23+
"""Convert a single Python script to a Jupyter notebook."""
24+
# Parse the Python file
25+
_file_conf, blocks = split_code_and_text_blocks(str(src_file))
26+
27+
# Convert to notebook (returns a dict, not a notebook object)
28+
example_nb_dict = jupyter_notebook(blocks, gallery_conf, str(src_file.parent))
29+
30+
# Convert dict to nbformat notebook object
31+
example_nb = nbformat.from_dict(example_nb_dict)
32+
33+
# Prepend an installation cell for moabb (unless the example already does so)
34+
first_source = ""
35+
if getattr(example_nb, "cells", None):
36+
try:
37+
first_source = example_nb.cells[0].source
38+
except (IndexError, AttributeError):
39+
first_source = ""
40+
41+
install_cmd = "%pip install moabb"
42+
if "pip install" not in first_source or "moabb" not in first_source:
43+
install_cell = nbformat.v4.new_code_cell(source=install_cmd)
44+
install_cell.metadata["language"] = "python"
45+
example_nb.cells.insert(0, install_cell)
46+
47+
output_file.parent.mkdir(parents=True, exist_ok=True)
48+
save_notebook(example_nb, output_file)
49+
50+
51+
def main() -> int:
52+
parser = argparse.ArgumentParser(
53+
description="Convert a Python example script to a Jupyter notebook."
54+
)
55+
parser.add_argument("--input", required=True, help="Path to the Python script.")
56+
parser.add_argument("--output", required=True, help="Path to the output notebook.")
57+
args = parser.parse_args()
58+
59+
input_path = Path(args.input)
60+
output_path = Path(args.output)
61+
62+
gallery_conf = copy.deepcopy(gen_gallery.DEFAULT_GALLERY_CONF)
63+
64+
convert_script_to_notebook(input_path, output_path, gallery_conf)
65+
print(f"Notebook saved to: {output_path}")
66+
return 0
67+
68+
69+
if __name__ == "__main__":
70+
raise SystemExit(main())

0 commit comments

Comments
 (0)