Skip to content

Instantly share code, notes, and snippets.

@kpsychas
Last active August 2, 2016 16:39
Show Gist options
  • Save kpsychas/f9f1c0629236065a5bf1 to your computer and use it in GitHub Desktop.
Save kpsychas/f9f1c0629236065a5bf1 to your computer and use it in GitHub Desktop.
Customizable run of ipython notebook from command line. Requires runipy (https://github.com/paulgb/runipy) library
#!/usr/bin/env python
import argparse
from runipy.notebook_runner import NotebookRunner
from IPython.nbformat.current import read
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--notebook',
type=str, required=True,
help='notebook to run')
parser.add_argument('-p', '--pylab',
action='store_true',
help='enable pylab')
parser.add_argument('-w', '--write',
action='store_true',
help='write result back')
args = parser.parse_args()
# add extension if not given
notebook = '{name}{ext}'.format(name=args.notebook, ext=''
if '.' in args.notebook else '.ipynb')
print('Checking: {}'.format(notebook))
notebook_data = read(open(notebook), 'json')
r = NotebookRunner(notebook_data, pylab=args.pylab)
r.run_notebook()
if args.write:
from IPython.nbformat.current import write
write(r.nb, open(notebook, 'w'), 'json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment