def run_cell(km, cell, timeout=20): shell = km.shell_channel iopub = km.iopub_channel shell.execute(cell.input) shell.get_msg(timeout=timeout) outs = [] while True: try: msg = iopub.get_msg(timeout=0.2) except Empty: break msg_type = msg['msg_type'] if msg_type in ('status', 'pyin'): continue elif msg_type == 'clear_output': outs = [] continue content = msg['content'] out = NotebookNode(output_type=msg_type) if msg_type == 'stream': out.stream = content['name'] out.txt = content['data'] elif msg_type in ('display_data', 'pyout'): for mime, data in content['data'].items(): attr = mime.split('/')[-1].lower() attr = attr.replace('+xml', '').replace('plain', 'text') setattr(out, attr, data) if msg_type == 'pyout': out.prompt_number = content['execution_count'] elif msg_type == 'pyerr': out.ename = content['ename'] out.evalue = content['evalue'] out.traceback = content['traceback'] else: print("unhandled iopub msg:", msg_type) outs.append(out) cell.outputs = outs return outs