-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_notebooks.py
47 lines (32 loc) · 1.28 KB
/
test_notebooks.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
from pathlib import Path
from typing import List
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors import CellExecutionError
def _process_notebook(fp: Path):
"""Checks if an IPython notebook runs without error from start to finish. If so, writes the notebook to HTML (with outputs) and overwrites the .ipynb file (without outputs)."""
with open(fp) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
# Check that the notebook runs
ep.preprocess(nb, {"metadata": {"path": "notebooks"}})
except CellExecutionError as e:
print(f"Failed executing {fp}")
print(e)
raise
print(f"Successfully executed {fp}")
return
def _get_all_notebooks_in_repo() -> List[Path]:
ROOT_DIR = Path(__file__).parent.parent.parent
NOTEBOOK_DIR = ROOT_DIR / "notebooks"
return list(NOTEBOOK_DIR.glob("*.ipynb"))
def test_notebook(notebook):
_process_notebook(notebook)
def pytest_generate_tests(metafunc):
notebooks = _get_all_notebooks_in_repo()
metafunc.parametrize("notebook", notebooks)
if __name__ == "__main__":
notebooks = _get_all_notebooks_in_repo()
for notebook in notebooks:
print(notebook)