Guest User

Untitled

a guest
Jun 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. # ! python
  2. # coding: utf-8
  3.  
  4. import os
  5. import argparse
  6. import glob
  7.  
  8. import nbformat
  9. from nbconvert.preprocessors import ExecutePreprocessor
  10. from nbconvert.preprocessors.execute import CellExecutionError
  11.  
  12. # Parse args
  13. parser = argparse.ArgumentParser(description="Runs a set of Jupyter \
  14. notebooks.")
  15. file_text = """ Notebook file(s) to be run, e.g. '*.ipynb' (default),
  16. 'my_nb1.ipynb', 'my_nb1.ipynb my_nb2.ipynb', 'my_dir/*.ipynb'
  17. """
  18. parser.add_argument('file_list', metavar='F', type=str, nargs='*',
  19. help=file_text)
  20. parser.add_argument('-t', '--timeout', help='Length of time (in secs) a cell \
  21. can run before raising TimeoutError (default 600).', default=600,
  22. required=False)
  23. parser.add_argument('-p', '--run-path', help='The path the notebook will be \
  24. run from (default pwd).', default='.', required=False)
  25. args = parser.parse_args()
  26. print('Args:', args)
  27. if not args.file_list: # Default file_list
  28. args.file_list = glob.glob('*.ipynb')
  29.  
  30. # Check list of notebooks
  31. notebooks = []
  32. print('Notebooks to run:')
  33. for f in args.file_list:
  34. # Find notebooks but not notebooks previously output from this script
  35. if f.endswith('.ipynb') and not f.endswith('_out.ipynb'):
  36. print(f[:-6])
  37. notebooks.append(f[:-6]) # Want the filename without '.ipynb'
  38.  
  39. # Execute notebooks and output
  40. for n in notebooks:
  41. n_out = n + '_out'
  42. with open(n + '.ipynb') as f:
  43. nb = nbformat.read(f, as_version=4)
  44. ep = ExecutePreprocessor(timeout=args.timeout, kernel_name='python3')
  45. try:
  46. print('Running', n)
  47. out = ep.preprocess(nb, {'metadata': {'path': args.run_path}})
  48. except CellExecutionError:
  49. out = None
  50. msg = 'Error executing the notebook "%s".\n' % n
  51. msg += 'See notebook "%s" for the traceback.' % n_out
  52. print(msg)
  53. except TimeoutError:
  54. msg = 'Timeout executing the notebook "%s".\n' % n
  55. print(msg)
  56. finally:
  57. # Write output file
  58. with open(n_out + '.ipynb', mode='wt') as f:
  59. nbformat.write(nb, f)
Add Comment
Please, Sign In to add comment