From 4f029f404ba9498541a12b4391de5234162a9d06 Mon Sep 17 00:00:00 2001 From: Mike Fienen Date: Sat, 3 Feb 2024 10:34:43 -0600 Subject: [PATCH 1/2] incorporated testing for notebook output prior to clearing output --- notebooks/clear_all_notebooks.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/notebooks/clear_all_notebooks.py b/notebooks/clear_all_notebooks.py index cd548d0..3bb016a 100644 --- a/notebooks/clear_all_notebooks.py +++ b/notebooks/clear_all_notebooks.py @@ -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 @@ -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") \ No newline at end of file From cf97eef60604dab78cd2bbdec581fd66ef998b07 Mon Sep 17 00:00:00 2001 From: Mike Fienen Date: Sat, 3 Feb 2024 10:39:49 -0600 Subject: [PATCH 2/2] typo in test_notebooks_output.py and added flopy project structured overview to xfail notebooks for testing due to important but incomplete code blocks --- tests/test_notebook_output.py | 2 +- tests/test_notebooks.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_notebook_output.py b/tests/test_notebook_output.py index 8265c33..c8103b9 100644 --- a/tests/test_notebook_output.py +++ b/tests/test_notebook_output.py @@ -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!") diff --git a/tests/test_notebooks.py b/tests/test_notebooks.py index b5017a6..9e56219 100644 --- a/tests/test_notebooks.py +++ b/tests/test_notebooks.py @@ -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' }