Skip to content

Commit 5bcb873

Browse files
committed
add automatic running of notebooks, describe in CONTRIBUTING file
1 parent f721c67 commit 5bcb873

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Contributing to Notebooks
2+
3+
## Docker Image Validation
4+
5+
The docker image must be able to run all notebooks. Whenever a change is made to
6+
the docker image, it must be validated. This can be accomplished by automatically
7+
running all of the notebooks using the supplied test script. From the root
8+
directory within the docker container, run:
9+
```
10+
$> pytest tests/test_notebooks.py
11+
```
12+
## Automated Running and Skipping
13+
14+
To enable validation of the Docker image, every notebook should run successfully
15+
when run from the command line. For notebooks where that just is not possible,
16+
the notebooks can be excluded from automated running by adding a file named
17+
`norun` to the notebook directory. Note that this results in the entire directory
18+
of notebooks being skipped. Excluding a notebook from automated running
19+
means that it is excluded from Docker Image validation. **If a notebook is
20+
skipped, it will not be guaranteed to be supported by the Docker image.**

planet-notebook-docker/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ numpy==1.14.* # pinned because numpy 15.0 throws runtime warning
88
opencv-python==3.3.0.10
99
planet
1010
pyproj
11+
pytest
1112
rasterio==1.0a12
1213
scikit-image
1314
scikit-learn

tests/test_notebooks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Run each notebook in the jupyter-notebooks directory.
3+
4+
Notebooks are run using nbconvert.
5+
6+
pytest runs each test as a unit test.
7+
"""
8+
import os
9+
import subprocess
10+
import tempfile
11+
12+
import pytest
13+
14+
NOTEBOOK_EXT = '.ipynb'
15+
ROOT_DIR = 'jupyter-notebooks'
16+
17+
18+
def find_notebooks(root_dir):
19+
print('finding notebooks...')
20+
notebooks = []
21+
for (dirpath, dirnames, filenames) in os.walk(root_dir):
22+
print('{}'.format(dirpath))
23+
24+
# look for 'norun' file, if present, do not look for notebooks in this dir
25+
if 'norun' in filenames:
26+
# do not look into subdirectories, modify in-place for walk()
27+
dirnames[:] = []
28+
29+
# clear filenames
30+
filenames = []
31+
32+
# modify in place to filter subdirectories in walk()
33+
# https://stackoverflow.com/questions/19859840/excluding-directories-in-os-walk
34+
dirnames[:] = [n for n in dirnames if n != '.ipynb_checkpoints']
35+
36+
for name in filenames:
37+
ext = os.path.splitext(name)[1]
38+
if ext == NOTEBOOK_EXT:
39+
# convert to string so test displays the path
40+
notebook_name = str(os.path.join(dirpath, name))
41+
notebooks.append(notebook_name)
42+
print('found {} notebooks'.format(len(notebooks)))
43+
return notebooks
44+
45+
46+
@pytest.mark.parametrize("notebook", find_notebooks(ROOT_DIR))
47+
def test_run_notebooks(notebook):
48+
with tempfile.NamedTemporaryFile(suffix=NOTEBOOK_EXT) as tmp_nb:
49+
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
50+
"--ExecutePreprocessor.timeout=600",
51+
notebook, "--output", tmp_nb.name]
52+
print(' '.join(args))
53+
subprocess.check_call(args)

0 commit comments

Comments
 (0)