Preprocess Markdown notebooks to convert them to IPyNB format.
(app: Sphinx, *args, **kwargs)
| 32 | |
| 33 | |
| 34 | def preprocess_notebooks(app: Sphinx, *args, **kwargs): |
| 35 | """Preprocess Markdown notebooks to convert them to IPyNB format.""" |
| 36 | |
| 37 | import jupytext |
| 38 | import nbformat |
| 39 | |
| 40 | print("Converting Markdown files to IPyNB...") |
| 41 | for path in (HERE / "regression").glob("*.md"): |
| 42 | if any(path.match(pattern) for pattern in exclude_patterns): |
| 43 | continue |
| 44 | nb = jupytext.read(str(path)) |
| 45 | |
| 46 | # In .md to .ipynb conversion, do not include any cells that have the |
| 47 | # jupyterlite_sphinx_strip tag |
| 48 | nb.cells = [ |
| 49 | cell for cell in nb.cells if "jupyterlite_sphinx_strip" not in cell.metadata.get("tags", []) |
| 50 | ] |
| 51 | |
| 52 | ipynb_path = path.with_suffix(".ipynb") |
| 53 | with open(ipynb_path, "w") as f: |
| 54 | nbformat.write(nb, f) |
| 55 | print(f"Converted {path} to {ipynb_path}") |
| 56 | |
| 57 | |
| 58 | # Should match {{ parent_docname }} or {{parent_docname}} |