summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorkennykos <“gkosmacher01@gmail.com”>2023-03-10 12:12:23 -0600
committerkennykos <“gkosmacher01@gmail.com”>2023-03-10 12:12:23 -0600
commitf0def882f41f7ea9414203c0c16524104fd9e206 (patch)
treebc6dc77dc456b077c5bfbeaab64e1e3c833c346a /bin
parent4bf1a4d92776b9a72e42b22a3f7252abdd00c81d (diff)
adding script to bu used in the workflow
Diffstat (limited to 'bin')
-rw-r--r--bin/run_notebooks.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/bin/run_notebooks.py b/bin/run_notebooks.py
new file mode 100644
index 0000000..ea7e8ee
--- /dev/null
+++ b/bin/run_notebooks.py
@@ -0,0 +1,67 @@
+# ! python
+# coding: utf-8
+# From https://gist.github.com/tpogden/ec79f2ebe2baf45655445b575dc7f540
+# with some more context at https://ogden.eu/run-notebooks
+# Thanks to @tpogden for his gist
+
+import os
+import sys
+import argparse
+import glob
+
+import nbformat
+from nbconvert.preprocessors import ExecutePreprocessor
+from nbconvert.preprocessors.execute import CellExecutionError
+
+# Parse args
+parser = argparse.ArgumentParser(description="Runs a set of Jupyter \
+ notebooks.")
+file_text = """ Notebook file(s) to be run, e.g. '*.ipynb' (default),
+'my_nb1.ipynb', 'my_nb1.ipynb my_nb2.ipynb', 'my_dir/*.ipynb'
+"""
+parser.add_argument('file_list', metavar='F', type=str, nargs='*',
+ help=file_text)
+parser.add_argument('-t', '--timeout', help='Length of time (in secs) a cell \
+ can run before raising TimeoutError (default 600).', default=600,
+ required=False)
+parser.add_argument('-p', '--run-path', help='The path the notebook will be \
+ run from (default pwd).', default='.', required=False)
+args = parser.parse_args()
+print('Args:', args)
+if not args.file_list: # Default file_list
+ args.file_list = glob.glob('*.ipynb')
+
+# Check list of notebooks
+notebooks = []
+print('Notebooks to run:')
+for f in args.file_list:
+ # Find notebooks but not notebooks previously output from this script
+ if f.endswith('.ipynb') and not f.endswith('_out.ipynb'):
+ print(f[:-6])
+ notebooks.append(f[:-6]) # Want the filename without '.ipynb'
+
+# Execute notebooks and output
+num_notebooks = len(notebooks)
+print('*****')
+for i, n in enumerate(notebooks):
+ n_out = n + '_out'
+ with open(n + '.ipynb') as f:
+ nb = nbformat.read(f, as_version=4)
+ ep = ExecutePreprocessor(timeout=int(args.timeout), kernel_name='python3')
+ try:
+ print('Running', n, ':', i, '/', num_notebooks)
+ out = ep.preprocess(nb, {'metadata': {'path': args.run_path}})
+ except CellExecutionError:
+ out = None
+ msg = 'Error executing the notebook "%s".\n' % n
+ msg += 'See notebook "%s" for the traceback.' % n_out
+ print(msg)
+ sys.exit(1)
+ except TimeoutError:
+ msg = 'Timeout executing the notebook "%s".\n' % n
+ print(msg)
+ sys.exit(110)
+ finally:
+ # Write output file
+ with open(n_out + '.ipynb', mode='wt') as f:
+ nbformat.write(nb, f) \ No newline at end of file