Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active April 9, 2022 16:51

Revisions

  1. minrk revised this gist Apr 27, 2013. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions checkipnb.py
    Original file line number Diff line number Diff line change
    @@ -11,13 +11,21 @@

    from Queue import Empty

    from IPython.kernel import KernelManager
    try:
    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()
    try:
    kc = km.client()
    except AttributeError:
    # 0.13
    kc = km
    kc.start_channels()
    shell = kc.shell_channel
    # simple ping:
  2. minrk revised this gist Apr 27, 2013. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions checkipnb.py
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,15 @@

    from Queue import Empty

    from IPython.zmq.blockingkernelmanager import BlockingKernelManager
    from IPython.kernel import KernelManager
    from IPython.nbformat.current import reads, NotebookNode

    def run_notebook(nb):
    km = BlockingKernelManager()
    km = KernelManager()
    km.start_kernel(stderr=open(os.devnull, 'w'))
    km.start_channels()
    shell = km.shell_channel
    kc = km.client()
    kc.start_channels()
    shell = kc.shell_channel
    # simple ping:
    shell.execute("pass")
    shell.get_msg()
    @@ -47,6 +48,7 @@ def run_notebook(nb):
    print " ran %3i cells" % cells
    if failures:
    print " %3i cells raised exceptions" % failures
    kc.stop_channels()
    km.shutdown_kernel()
    del km

    @@ -55,4 +57,4 @@ def run_notebook(nb):
    print "running %s" % ipynb
    with open(ipynb) as f:
    nb = reads(f.read(), 'json')
    run_notebook(nb)
    run_notebook(nb)
  3. minrk created this gist May 6, 2012.
    58 changes: 58 additions & 0 deletions checkipnb.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #!/usr/bin/env python
    """
    simple example script for running notebooks and reporting exceptions.
    Usage: `checkipnb.py 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.zmq.blockingkernelmanager import BlockingKernelManager
    from IPython.nbformat.current import reads, NotebookNode

    def run_notebook(nb):
    km = BlockingKernelManager()
    km.start_kernel(stderr=open(os.devnull, 'w'))
    km.start_channels()
    shell = km.shell_channel
    # simple ping:
    shell.execute("pass")
    shell.get_msg()

    cells = 0
    failures = 0
    for ws in nb.worksheets:
    for cell in ws.cells:
    if cell.cell_type != 'code':
    continue
    shell.execute(cell.input)
    # 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
    sys.stdout.write('.')

    print
    print "ran notebook %s" % nb.metadata.name
    print " ran %3i cells" % cells
    if failures:
    print " %3i cells raised exceptions" % failures
    km.shutdown_kernel()
    del km

    if __name__ == '__main__':
    for ipynb in sys.argv[1:]:
    print "running %s" % ipynb
    with open(ipynb) as f:
    nb = reads(f.read(), 'json')
    run_notebook(nb)