Skip to content

Commit d258150

Browse files
committed
Add image/load checker
1 parent c938aac commit d258150

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

tools/check_load_and_images.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Iterate through and execute all notebooks
3+
"""
4+
from collections import defaultdict
5+
from pathlib import Path
6+
import random
7+
import re
8+
9+
10+
def main():
11+
p = Path('.')
12+
notebook_paths = p.glob('**/*.ipynb')
13+
patterns = dict(
14+
loads=re.compile(r'%load ([^\s]+\.py)'),
15+
images=re.compile(r'\((images/[^\s]+(?:png|jpg|jpeg))\)')
16+
)
17+
exceptions = defaultdict(list)
18+
19+
for nb_path in notebook_paths:
20+
if '.ipynb_checkpoints' in str(nb_path):
21+
continue
22+
print(nb_path)
23+
with open(nb_path) as f:
24+
contents = f.read()
25+
for name, pattern in patterns.items():
26+
print(f'Checking {name}...')
27+
matches = pattern.findall(contents)
28+
for match in matches:
29+
print(f'\t{match}')
30+
p = nb_path.parent / match
31+
try:
32+
with open(p) as _:
33+
pass
34+
except FileNotFoundError as e:
35+
exceptions[str(nb_path)].append(str(e))
36+
37+
if exceptions:
38+
print('⚠️' * 20)
39+
print('\n\nFailures in these notebooks:')
40+
print('\n'.join(exceptions.keys()), '\n\n')
41+
print('⚠️' * 20)
42+
for nb, error_list in exceptions.items():
43+
face = random.choice(["😮", "😱", "🤬"])
44+
print(face * 20)
45+
errors = "\n".join(error_list)
46+
print(f'{nb}: \n{errors}')
47+
print(face * 20)
48+
else:
49+
print('🎉🎉 Everything worked! 🎉🎉')
50+
51+
52+
if __name__ == '__main__':
53+
main()

tools/execute_notebooks.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
"""
22
Iterate through and execute all notebooks
33
"""
4+
from collections import defaultdict
45
from pathlib import Path
6+
import random
57

68
import nbformat as nbf
79
from nbconvert.preprocessors import ExecutePreprocessor
810

911

12+
def print_exceptions(exceptions):
13+
if exceptions:
14+
print('⚠️' * 20)
15+
print('\n\nFailures in these notebooks:')
16+
print('\n'.join(exceptions.keys()), '\n\n')
17+
print('⚠️' * 20)
18+
for nb, error_list in exceptions.items():
19+
face = random.choice(["😮", "😱", "🤬"])
20+
print(face * 20)
21+
errors = "\n".join(error_list)
22+
print(f'{nb}: \n{errors}')
23+
print(face * 20)
24+
else:
25+
print('🎉🎉 Everything worked! 🎉🎉')
26+
27+
1028
def main():
1129
p = Path('.')
1230
notebook_paths = p.glob('**/*.ipynb')
13-
exceptions = {}
31+
exceptions = defaultdict(list)
1432
for nb_path in notebook_paths:
1533
if '.ipynb_checkpoints' in str(nb_path):
1634
continue
@@ -22,17 +40,9 @@ def main():
2240
ep.preprocess(notebook)
2341
print('Done executing')
2442
except Exception as e:
25-
exceptions[str(nb_path)] = e
43+
exceptions[str(nb_path)].append(str(e))
44+
print_exceptions(exceptions)
2645

27-
if exceptions:
28-
print('⚠️' * 20)
29-
print('\n\nFailures in these notebooks:')
30-
print('\n'.join(exceptions.keys()), '\n\n')
31-
print('⚠️' * 20)
32-
for nb, error in exceptions.items():
33-
print(f'{nb}: \n{error}')
34-
else:
35-
print('🎉🎉 Everything worked! 🎉🎉')
3646

3747

3848
if __name__ == '__main__':

0 commit comments

Comments
 (0)