Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

work on output clearing #111

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions notebooks/clear_all_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import pathlib as pl
import json

notebook_count = 0
# add the basenames of any notebooks we want to keep the results of
# all solutions are still not being cleared
Expand All @@ -14,12 +16,27 @@
'10_modpath-demo.ipynb'
]

def check_nb_for_output(notebook):
has_output = False
# run autotest on each notebook
with open(notebook) as src:
data = src.read()
data = json.loads(data)
for item in data['cells']:
for k, v in item.items():
if item['cell_type'] == 'code':
if any (item['outputs']):
has_output = True
return has_output

for [path, subdirs, files] in os.walk('.'):
for cf in files:
if cf.lower().endswith('.ipynb') and '.ipynb_checkpoint' not in path:
nb = pl.Path(path) / cf
if 'solutions' not in str(nb) and nb.name not in skip_notebooks:
print(f"clearing {nb}")
os.system(f"jupyter nbconvert --clear-output --inplace {nb._str}")
notebook_count += 1
if ('solutions' not in str(nb) and
nb.name not in skip_notebooks and
check_nb_for_output(nb) is True):
print(f"clearing {nb}")
os.system(f"jupyter nbconvert --clear-output --inplace {nb._str}")
notebook_count += 1
print(notebook_count," notebooks cleared")
2 changes: 1 addition & 1 deletion tests/test_notebook_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ def test_notebook_output(notebook):
for item in data['cells']:
for k, v in item.items():
if item['cell_type'] == 'code':
assert not any (item['outputs']), (f"{notebook} as output "
assert not any (item['outputs']), (f"{notebook} has output "
"but is not listed in notebooks/clean_all_notebooks.py "
"as excepted!")
1 change: 1 addition & 0 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'06_matplotlib.ipynb': 'notebook intentionally incomplete',
'00_python_basics_review__solutions.ipynb': 'intentional error',
'01_functions_script__solution.ipynb': 'intentional error',
'06-Project-structured.ipynb': 'intentionally incomplete information'


}
Expand Down
Loading