forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_myst_doc.py
84 lines (64 loc) · 2.24 KB
/
test_myst_doc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Convert a jupytext-compliant format in to a python script
and execute it with parsed arguments.
Any cell with 'remove-cell-ci' tag in metadata will not be included
in the converted python script.
"""
import argparse
import subprocess
import sys
import tempfile
from pathlib import Path
import jupytext
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--path",
help="path to the jupytext-compatible file",
)
parser.add_argument(
"--find-recursively",
action="store_true",
help="if true, will attempt to find path recursively in cwd",
)
parser.add_argument(
"--no-postprocess",
action="store_true",
help="if true, will not postprocess the notebook",
)
def filter_out_cells_with_remove_cell_ci_tag(cells: list):
"""Filters out cells which contain the 'remove-cell-ci' tag in metadata"""
def should_keep_cell(cell):
tags = cell.metadata.get("tags")
if tags:
# Both - and _ for consistent behavior with built-in tags
return "remove_cell_ci" not in tags and "remove-cell-ci" not in tags
return True
return [cell for cell in cells if should_keep_cell(cell)]
def postprocess_notebook(notebook):
notebook.cells = filter_out_cells_with_remove_cell_ci_tag(notebook.cells)
return notebook
DISPLAY_FUNCTION = """
def display(*args, **kwargs):
print(*args, **kwargs)
"""
if __name__ == "__main__":
args, remainder = parser.parse_known_args()
path = Path(args.path)
cwd = Path.cwd()
if args.find_recursively and not path.exists():
path = next((p for p in cwd.rglob("*") if str(p).endswith(args.path)), None)
assert path and path.exists()
with open(path, "r") as f:
notebook = jupytext.read(f)
if not args.no_postprocess:
notebook = postprocess_notebook(notebook)
name = ""
with tempfile.NamedTemporaryFile("w", delete=False) as f:
# Define the display function, which is available in notebooks,
# but not in normal Python scripts.
f.write(DISPLAY_FUNCTION)
jupytext.write(notebook, f, fmt="py:percent")
name = f.name
remainder.insert(0, name)
remainder.insert(0, sys.executable)
# Run the notebook
subprocess.run(remainder, check=True)