Convert Python demos in Jupytext 'light' into MyST markdown/ipynb. Uses Jupytext. These files can then be included in Sphinx documentation.
()
| 14 | |
| 15 | |
| 16 | def process(): |
| 17 | """Convert Python demos in Jupytext 'light' into MyST markdown/ipynb. |
| 18 | |
| 19 | Uses Jupytext. These files can then be included in Sphinx |
| 20 | documentation. |
| 21 | """ |
| 22 | # Directories to scan |
| 23 | subdirs = [pathlib.Path("../../demo")] |
| 24 | |
| 25 | # Iterate over subdirectories containing demos |
| 26 | for subdir in subdirs: |
| 27 | # Make demo doc directory |
| 28 | demo_dir = pathlib.Path("./demos") |
| 29 | demo_dir.mkdir(parents=True, exist_ok=True) |
| 30 | |
| 31 | # Process each demo using jupytext/myst |
| 32 | for demo in subdir.glob("**/demo*.py"): |
| 33 | python_demo = jupytext.read(demo) |
| 34 | myst_text = jupytext.writes(python_demo, fmt="myst") |
| 35 | |
| 36 | # myst-parser does not process blocks with {code-cell} |
| 37 | myst_text = myst_text.replace("{code-cell}", "python") |
| 38 | myst_file = (demo_dir / demo.name).with_suffix(".md") |
| 39 | with open(myst_file, "w") as fw: |
| 40 | fw.write(myst_text) |
| 41 | |
| 42 | ipynb_file = (demo_dir / demo.name).with_suffix(".ipynb") |
| 43 | jupytext.write(python_demo, ipynb_file, fmt="ipynb") |
| 44 | |
| 45 | # Copy python demo files into documentation demo directory |
| 46 | shutil.copy(demo, demo_dir) |
| 47 | |
| 48 | # If demo saves matplotlib images, run the demo to create |
| 49 | # images |
| 50 | code = demo.read_text() |
| 51 | if "savefig" in code: |
| 52 | demo = demo.resolve() |
| 53 | here = os.getcwd() |
| 54 | os.chdir(demo_dir) |
| 55 | runpy.run_path(str(demo)) |
| 56 | os.chdir(here) |
| 57 | |
| 58 | # Copy images used in demos |
| 59 | for file in subdir.glob("**/*.png"): |
| 60 | shutil.copy(file, demo_dir) |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
no test coverage detected