Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@minrk /
Last active

run and validate a notebook
#!/usr/bin/env python
simple example script for running notebooks and reporting exceptions.
Usage: ` foo.ipynb [bar.ipynb [...]]`
Each cell is submitted to the kernel, and checked for errors.
import os,sys,time
from Queue import Empty
from IPython.kernel import KernelManager
except ImportError:
from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager
from IPython.nbformat.current import reads, NotebookNode
def run_notebook(nb):
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
kc = km.client()
except AttributeError:
# 0.13
kc = km
shell = kc.shell_channel
# simple ping:
cells = 0
failures = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
# wait for finish, maximum 20s
reply = shell.get_msg(timeout=20)['content']
if reply['status'] == 'error':
failures += 1
print "\nFAILURE:"
print cell.input
print '-----'
print "raised:"
print '\n'.join(reply['traceback'])
cells += 1
print "ran notebook %s" %
print " ran %3i cells" % cells
if failures:
print " %3i cells raised exceptions" % failures
del km
if __name__ == '__main__':
for ipynb in sys.argv[1:]:
print "running %s" % ipynb
with open(ipynb) as f:
nb = reads(, 'json')

If I have a notebook that has cells ready to be run (say they produce plots form an experiment), and I run this on that notebook, when I open the notebook, the output is still note created. Is it possible to run the notebook offline, and then overwrite the nb with the newly run version?


@hugadams I had the same question, and I discovered this:


Neither the above code, nor runipy, seem to run actually run an IPython notebook as the notebook itself runs it: if I have a "%load" directive in a notebook, and I run all the cells via the interactive menu, the "%load" magic loads the code in a new cell, and this cell is then executed. On the opposite, the various headless runners that I have found just skip the new cell.

It would be really useful to be able to run notebook headless. Currently it is hard to do quality assurance on notebooks.


@minrk would it be possible to update this script for IPython 3?


@minrk Second that request. Tried to run but got a:

AttributeError: 'ZMQSocketChannel' object has no attribute 'execute'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.