11import os
22import pathlib as pl
33import json
4+ import subprocess
45
56notebook_count = 0
67# add the basenames of any notebooks we want to keep the results of
1819 '10_modpath_particle_tracking-demo.ipynb'
1920 ]
2021
21- def check_nb_for_output (notebook ):
22- has_output = False
23- # run autotest on each notebook
24- with open (notebook ) as src :
25- data = src .read ()
26- data = json .loads (data )
27- for item in data ['cells' ]:
28- for k , v in item .items ():
29- if item ['cell_type' ] == 'code' :
30- if any (item ['outputs' ]):
31- has_output = True
32- return has_output
33-
34-
3522if __name__ == "__main__" :
36-
37- for [path , subdirs , files ] in os .walk ('.' ):
38- for cf in files :
39- if cf .lower ().endswith ('.ipynb' ) and '.ipynb_checkpoint' not in path :
40- nb = pl .Path (path ) / cf
41- if ('solutions' not in str (nb ) and
42- nb .name not in skip_notebooks and
43- check_nb_for_output (nb ) is True ):
44- print (f"clearing { nb } " )
45- os .system (f"jupyter nbconvert --clear-output --inplace { nb ._str } " )
46- notebook_count += 1
47- print (notebook_count ," notebooks cleared" )
23+ nbdir = pl .Path ("." )
24+ nbs = nbdir .rglob ("*.ipynb" )
25+ for nb in nbs :
26+ if ('solutions' not in str (nb ) and
27+ nb .name not in skip_notebooks ):
28+ print ("clearing" , nb )
29+ cmd = (
30+ "jupyter" ,
31+ "nbconvert" ,
32+ "--ClearOutputPreprocessor.enabled=True" ,
33+ "--ClearMetadataPreprocessor.enabled=True" ,
34+ "--ClearMetadataPreprocessor.preserve_nb_metadata_mask={('kernelspec')}" ,
35+ "--inplace" ,
36+ nb ,
37+ )
38+ proc = subprocess .run (cmd )
39+ assert proc .returncode == 0 , f"Error running command: { ' ' .join (cmd )} "
40+
0 commit comments