Skip to content

Commit ebea16f

Browse files
authored
Merge pull request #155 from mwcraig/little-fixes
A few little fixes for issues identified by the loads/images checker
2 parents 3e309c6 + a10ae06 commit ebea16f

File tree

5 files changed

+66
-13
lines changed

5 files changed

+66
-13
lines changed

notebooks/02.Interact/02.00-Using-Interact.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
"metadata": {},
298298
"outputs": [],
299299
"source": [
300-
"# %load solutions/interact-basic-list/reverse-text.py\n"
300+
"# %load solutions/reverse-text.py\n"
301301
]
302302
},
303303
{
@@ -435,7 +435,7 @@
435435
"metadata": {},
436436
"outputs": [],
437437
"source": [
438-
"# %load solutions/interact-basic-list/plot-function.py"
438+
"# %load solutions/plot-function.py"
439439
]
440440
},
441441
{

tools/check_load_and_images.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
from execute_notebooks import print_exceptions
10+
11+
12+
def main():
13+
p = Path('.')
14+
notebook_paths = p.glob('**/*.ipynb')
15+
patterns = dict(
16+
loads=re.compile(r'%load ([^\s]+\.py)'),
17+
images=re.compile(r'\((images/[^\s]+(?:png|jpg|jpeg))\)')
18+
)
19+
exceptions = defaultdict(list)
20+
21+
for nb_path in notebook_paths:
22+
if '.ipynb_checkpoints' in str(nb_path):
23+
continue
24+
print(nb_path)
25+
with open(nb_path) as f:
26+
contents = f.read()
27+
for name, pattern in patterns.items():
28+
print(f'Checking {name}...')
29+
matches = pattern.findall(contents)
30+
for match in matches:
31+
print(f'\t{match}')
32+
p = nb_path.parent / match
33+
try:
34+
with open(p) as _:
35+
pass
36+
except FileNotFoundError as e:
37+
exceptions[str(nb_path)].append(str(e))
38+
39+
print_exceptions(exceptions)
40+
41+
42+
if __name__ == '__main__':
43+
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)