def execute_notebook(nb): km = BlockingKernelManager() km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) km.start_channels() # run %pylab inline, because some notebooks assume this # even though they shouldn't km.shell_channel.execute("pass") km.shell_channel.get_msg() while True: try: km.sub_channel.get_msg(timeout=1) except Empty: break successes = 0 failures = 0 errors = 0 prompt_number = 1 for ws in nb.worksheets: for cell in ws.cells: cell.prompt_number = prompt_number if cell.cell_type != 'code': continue try: outs = run_cell(km, cell) except Exception as e: print "failed to run cell:", repr(e) print cell.input errors += 1 continue cell.outputs = outs prompt_number += 1 km.shutdown_kernel() del km
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
def test_notebook(nb): km = BlockingKernelManager() km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) km.start_channels() # run %pylab inline, because some notebooks assume this # even though they shouldn't km.shell_channel.execute("pass") km.shell_channel.get_msg() while True: try: km.sub_channel.get_msg(timeout=1) except Empty: break successes = 0 failures = 0 errors = 0 for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type != 'code': continue try: outs = run_cell(km, cell) except Exception as e: print "failed to run cell:", repr(e) print cell.input errors += 1 continue failed = False for out, ref in zip(outs, cell.outputs): if not compare_outputs(out, ref): failed = True if failed: failures += 1 else: successes += 1 sys.stdout.write('.') print print "tested notebook %s" % nb.metadata.name print " %3i cells successfully replicated" % successes if failures: print " %3i cells mismatched output" % failures if errors: print " %3i cells failed to complete" % errors km.shutdown_kernel() del km
def new_kernel(): """start a kernel in a subprocess, and wait for it to be ready Returns ------- kernel_manager: connected KernelManager instance """ KM = BlockingKernelManager() KM.start_kernel(stdout=PIPE, stderr=PIPE) KM.start_channels() # wait for kernel to be ready KM.shell_channel.execute("import sys") KM.shell_channel.get_msg(block=True, timeout=5) flush_channels(KM) try: yield KM finally: KM.stop_channels() KM.shutdown_kernel()
class IPyNbFile(pytest.File): def collect(self): with self.fspath.open() as f: self.nb = reads(f.read(), 'json') cell_num = 0 for ws in self.nb.worksheets: for cell in ws.cells: if cell.cell_type == "code": yield IPyNbCell(self.name, self, cell_num, cell) cell_num += 1 def setup(self): self.km = BlockingKernelManager() self.km.start_kernel(stderr=open(os.devnull, 'w')) self.km.start_channels() self.shell = self.km.shell_channel def teardown(self): self.km.shutdown_kernel() del self.shell del self.km
def test_notebook(nb, halt_on_error=True, km=None): our_kernel = km is None if our_kernel: km = BlockingKernelManager() km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) km.start_channels() print km.connection_file # run %pylab inline, because some notebooks assume this # even though they shouldn't km.shell_channel.execute("pass") km.shell_channel.get_msg() km.restart_kernel() km.shell_channel.get_msg() while True: try: km.sub_channel.get_msg(timeout=1) except Empty: break successes = 0 failures = 0 errors = 0 halted = False for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type != 'code': continue try: outs, exec_count = run_cell(km, cell) except Exception as e: log.critical( "failed to run cell:"+ repr(e)) log.critical("tested notebook %s" % nb.metadata.name) print cell.input errors += 1 continue c = '.' for out in outs: if out['output_type'] == 'pyerr': c = 'E' if log.getEffectiveLevel() < logging.FATAL: sys.stderr.write(c) failed = False #for out, ref in zip(outs, cell.outputs): # if not compare_outputs(out, ref): # failed = True if failed: failures += 1 else: successes += 1 cell.outputs = outs cell.prompt_number = exec_count if c == 'E' and halt_on_error: log.info("halting on error") halted = True # XXX: this next part is specific to nbexplode save_intermediate(km, ws, cell, nb.metadata.name) break; if log.getEffectiveLevel() <= logging.WARNING: sys.stderr.write('\n') log.info("tested notebook %s" % nb.metadata.name) log.info(" %3i cells successfully replicated" % successes) if failures: log.info(" %3i cells mismatched output" % failures) if errors: log.info(" %3i cells failed to complete" % errors) if our_kernel: km.shutdown_kernel() del km return nb, halted
default=False, action='store_true', help="Try to run all cells (by default will halt on first error)") parser.add_argument('-A', '--allnotebooks', '--all-notebooks', default=False, action='store_true', help="Try to run all notebook (by default will halt on first error)") args = parser.parse_args() if args.verbose: log.setLevel(logging.INFO) if args.quiet: log.setLevel(logging.CRITICAL) km = BlockingKernelManager() km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) km.start_channels() if args.saveonerror: # XXX: hack - just dange an attribute we can check against km.saveonerror = True halt_on_error = not args.all halt_notebooks = not args.allnotebooks for ipynb in args.inputs: log.info('Running '+ ipynb) with open(ipynb) as f: nb = reads(f.read(), 'json') # here, before running the notebook, it'd be nice if we verified it's # metadata.name, and adjusted it according to the current filename, so