Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/scripts/convert_to_notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Convert Python example scripts to Jupyter notebooks for Colab integration.

This script converts Sphinx-Gallery style Python examples to Jupyter notebooks.
It is adapted from Braindecode's docs workflow utilities.
"""

from __future__ import annotations

import argparse
import copy
from pathlib import Path

import nbformat
from sphinx_gallery import gen_gallery
from sphinx_gallery.notebook import jupyter_notebook, save_notebook
from sphinx_gallery.py_source_parser import split_code_and_text_blocks


def convert_script_to_notebook(
src_file: Path, output_file: Path, gallery_conf: dict
) -> None:
"""Convert a single Python script to a Jupyter notebook."""
# Parse the Python file
_file_conf, blocks = split_code_and_text_blocks(str(src_file))

# Convert to notebook (returns a dict, not a notebook object)
example_nb_dict = jupyter_notebook(blocks, gallery_conf, str(src_file.parent))

# Convert dict to nbformat notebook object
example_nb = nbformat.from_dict(example_nb_dict)

# Prepend an installation cell for moabb (unless the example already does so)
first_source = ""
if getattr(example_nb, "cells", None):
try:
first_source = example_nb.cells[0].source
except (IndexError, AttributeError):
first_source = ""

install_cmd = "%pip install moabb"
if "pip install" not in first_source or "moabb" not in first_source:
install_cell = nbformat.v4.new_code_cell(source=install_cmd)
install_cell.metadata["language"] = "python"
example_nb.cells.insert(0, install_cell)

output_file.parent.mkdir(parents=True, exist_ok=True)
save_notebook(example_nb, output_file)


def main() -> int:
parser = argparse.ArgumentParser(
description="Convert a Python example script to a Jupyter notebook."
)
parser.add_argument("--input", required=True, help="Path to the Python script.")
parser.add_argument("--output", required=True, help="Path to the output notebook.")
args = parser.parse_args()

input_path = Path(args.input)
output_path = Path(args.output)

gallery_conf = copy.deepcopy(gen_gallery.DEFAULT_GALLERY_CONF)

convert_script_to_notebook(input_path, output_path, gallery_conf)
print(f"Notebook saved to: {output_path}")
return 0


if __name__ == "__main__":
raise SystemExit(main())
14 changes: 14 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ jobs:
run: |
cd docs && make html
- name: Generate notebooks from examples (Colab)
run: |
set -euo pipefail
echo "Converting Python examples to notebooks in docs/build/html/auto_examples/_notebooks..."
mkdir -p docs/build/html/auto_examples/_notebooks
find examples -type f -name '*.py' | while read -r f; do
rel="${f#examples/}"
out_dir="docs/build/html/auto_examples/_notebooks/$(dirname "$rel")"
mkdir -p "$out_dir"
base="$(basename "$rel" .py)"
out_path="$out_dir/$base.ipynb"
python .github/scripts/convert_to_notebook.py --input "$f" --output "$out_path"
done
# Create an artifact of the html output.
- uses: actions/upload-artifact@v4
with:
Expand Down
9 changes: 6 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cff-version: 1.3.0
cff-version: 1.4
message: "If you use this software, please cite it as below."
authors:
- family-names: "Aristimunha"
Expand Down Expand Up @@ -51,6 +51,9 @@ authors:
- family-names: "Goncharenko"
given-names: "Vladislav"
orcid: "https://orcid.org/0000-0002-9243-6914"
- family-names: "Andreev"
given-names: "Anton"
orcid: "https://orcid.org/000-0002-4466-4525"
- family-names: "Thielen"
given-names: "Jordy"
orcid: "https://orcid.org/0000-0002-6264-0367"
Expand All @@ -70,7 +73,7 @@ authors:
given-names: "Sylvain"
orcid: "https://orcid.org/0000-0003-3027-8241"
title: "Mother of all BCI Benchmarks"
version: 1.4.0
version: 1.4.3
doi: 10.5281/zenodo.10034223
date-released: 2025-11-07
date-released: 2025-12-12
url: "https://github.com/NeuroTechX/moabb"
Loading
Loading