def output(self, outs: t.List, msg: t.Dict, display_id: str, cell_index: int) -> t.Optional[t.List]: msg_type = msg['msg_type'] parent_msg_id = msg['parent_header'].get('msg_id') if self.output_hook_stack[parent_msg_id]: # if we have a hook registered, it will override our # default output behaviour (e.g. OutputWidget) hook = self.output_hook_stack[parent_msg_id][-1] hook.output(outs, msg, display_id, cell_index) return None try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) return None if self.clear_before_next_output: self.log.debug('Executing delayed clear_output') outs[:] = [] self.clear_display_id_mapping(cell_index) self.clear_before_next_output = False if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) outs.append(out) return out
def output(self, outs, msg, display_id, cell_index): msg_type = msg['msg_type'] try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) return if self.clear_before_next_output: self.log.debug('Executing delayed clear_output') outs[:] = [] self.clear_display_id_mapping(cell_index) self.clear_before_next_output = False if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) outs.append(out) return out
def output(self, outs, msg, display_id, cell_index): msg_type = msg['msg_type'] try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) return if self.clear_before_next_output: self.log.debug('Executing delayed clear_output') outs[:] = [] self.clear_display_id_mapping(cell_index) self.clear_before_next_output = False if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) outs.append(out) return out
def output(self, outs: t.List, msg: t.Dict) -> t.Optional[t.List]: try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg["msg_type"]) return outs.append(out)
def call_handlers(self, msg): msg_type = msg.get('msg_type',None) content = msg.get('content', None) buffers = msg.get('buffers', []) node = None if msg_type == 'execute_input': self.executions[int(content['execution_count'])] = content['code'] elif msg_type == 'error': node = output_from_msg(msg) # Capture print output elif msg_type == 'stream': node = output_from_msg(msg) elif msg_type == 'status': pass # e.g content['execution_state'] == 'idle' elif msg_type == "comm_open": self.queue.put(({'content':content, 'buffers':buffers}, 'comm_open')) return elif msg_type == "comm_msg": self.queue.put(({'content':content, 'buffers':buffers}, 'comm_msg')) return elif msg_type.startswith('comm'): print("Unhandled 'comm' message of type {msg_type} with {content}".format( msg_type=msg_type, content=content)) display_id = None if msg_type in {'execute_result', 'display_data', 'update_display_data'}: display_id = msg['content'].get('transient', {}).get('display_id', None) if msg_type == 'update_display_data': print("Unhandled 'update_display_data' message") # Should filter text/plain and text/html at this level # print(output_from_msg(msg)['data']) node = output_from_msg(msg) execution_count = list(self.executions.keys())[-1] if len(self.executions) else None if node: self.queue.put((node, execution_count)) elif msg_type == 'execute_reply': self.queue.put((None, execution_count))
def collect(self, msg): msg_type = msg['msg_type'] logging.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: self.cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': return else: return elif msg_type == 'execute_input': return elif msg_type == 'clear_output': self.outs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if self.cell_index in cell_map: cell_map[self.cell_index] = [] return elif msg_type.startswith('comm'): return display_id = None if msg_type in { 'execute_result', 'display_data', 'update_display_data' }: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded return try: out = output_from_msg(msg) except ValueError: logging.error("unhandled iopub msg: " + msg_type) return if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(self.cell_index, []) output_idx_list.append(len(self.outs)) self.outs.append(out)
def output( self, outs: t.List, msg: t.Dict, display_id: str, cell_index: int ) -> t.Optional[t.List]: msg_type = msg["msg_type"] try: out = output_from_msg(msg) self.log_output_message(out) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) return None return super().output(outs, msg, display_id, cell_index)
def collect(self, msg): msg_type = msg['msg_type'] logging.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: self.cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': return else: return elif msg_type == 'execute_input': return elif msg_type == 'clear_output': self.outs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if self.cell_index in cell_map: cell_map[self.cell_index] = [] return elif msg_type.startswith('comm'): return display_id = None if msg_type in {'execute_result', 'display_data', 'update_display_data'}: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded return try: out = output_from_msg(msg) except ValueError: logging.error("unhandled iopub msg: " + msg_type) return if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(self.cell_index, []) output_idx_list.append(len(self.outs)) self.outs.append(out)
def handle_iopub_for_msg(self, cell, msg_id): """ Process additional data sent by kernel (output, ...) """ outs = [] while True: try: msg = self.kc.iopub_channel.get_msg(timeout=self.timeout) except Empty: self.log("Timeout waiting for IOPub output") break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] content = msg['content'] # set the prompt number for the input and the output if cell and 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log("unhandled iopub msg: " + msg_type) else: self.log_debug("output: %s" % out) outs.append(out) return outs
def handle_iopub_for_msg(self, cell, msg_id): """ Process additional data sent by kernel (output, ...) """ outs = [] while True: try: msg = self.kc.iopub_channel.get_msg(timeout=self.timeout) except Empty: self.log("Timeout waiting for IOPub output") break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] content = msg['content'] # set the prompt number for the input and the output if cell and 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log("unhandled iopub msg: " + msg_type) else: self.log_debug("output: %s" % out) outs.append(out) return outs
def _update_display_id(self, display_id, msg): """Update outputs with a given display_id""" if display_id not in self._display_id_map: self.log.debug("display id %r not in %s", display_id, self._display_id_map) return if msg['header']['msg_type'] == 'update_display_data': msg['header']['msg_type'] = 'display_data' try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg['msg_type']) return for cell_idx, output_indices in self._display_id_map[display_id].items(): cell = self.nb['cells'][cell_idx] outputs = cell['outputs'] for output_idx in output_indices: outputs[output_idx]['data'] = out['data'] outputs[output_idx]['metadata'] = out['metadata']
def _update_display_id(self, display_id, msg): """Update outputs with a given display_id""" if display_id not in self._display_id_map: self.log.debug("display id %r not in %s", display_id, self._display_id_map) return if msg['header']['msg_type'] == 'update_display_data': msg['header']['msg_type'] = 'display_data' try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg['msg_type']) return for cell_idx, output_indices in self._display_id_map[display_id].items(): cell = self.nb['cells'][cell_idx] outputs = cell['outputs'] for output_idx in output_indices: outputs[output_idx]['data'] = out['data'] outputs[output_idx]['metadata'] = out['metadata']
def output(self, outs, msg, display_id, cell_index): if self.clear_before_next_output: self.outputs = [] self.clear_before_next_output = False self.parent_header = msg['parent_header'] output = output_from_msg(msg) if self.outputs: # try to coalesce/merge output text last_output = self.outputs[-1] if (last_output['output_type'] == 'stream' and output['output_type'] == 'stream' and last_output['name'] == output['name']): last_output['text'] += output['text'] else: self.outputs.append(output) else: self.outputs.append(output) self.sync_state() if hasattr(self.executor, 'widget_state'): # sync the state to the nbconvert state as well, since that is used for testing self.executor.widget_state[self.comm_id]['outputs'] = self.outputs
def call_handlers(self, msg): msg_type = msg.get('msg_type', None) content = msg.get('content', None) buffers = msg.get('buffers', []) metadata = msg.get('metadata', None) if msg_type == 'complete_reply': filtered = {} for field in ['matches', 'cursor_start', 'cursor_end']: if field == 'matches': value = [ el for el in content["matches"] if not (el.startswith("%") # Remove magics or el.startswith("_")) ] # And underscore methods else: value = content[field] filtered[field] = value self.queue.put((filtered, "completion")) return node = None if msg_type == 'execute_input': assert 'execution_count' in content try: executions = int(content['execution_count']) self.executions = executions except: logging.info('ERROR: execution_count is not an integer %r' % content['execution_count']) self.executions = None elif msg_type == 'error': node = output_from_msg(msg) # Capture print output elif msg_type == 'stream': node = output_from_msg(msg) elif msg_type == 'status': pass # e.g content['execution_state'] == 'idle' elif msg_type == "comm_open": self.queue.put(({ 'content': content, 'buffers': buffers, 'metadata': metadata }, 'comm_open')) return elif msg_type == "comm_msg": self.queue.put(({ 'content': content, 'buffers': buffers, 'metadata': metadata }, 'comm_msg')) return elif msg_type.startswith('comm'): logging.info( "Unhandled 'comm' message of type {msg_type} with {content}". format(msg_type=msg_type, content=content)) if msg_type in { 'execute_result', 'display_data', 'update_display_data' }: if msg_type == 'update_display_data': logging.info("Unhandled 'update_display_data' message") # Should filter text/plain and text/html at this level # print(output_from_msg(msg)['data']) node = output_from_msg(msg) if node: self.queue.put((node, self.executions)) elif msg_type == 'execute_reply': self.queue.put((None, self.execution_number()))
def run_cell(self, cell): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) # wait for finish, with timeout while True: try: timeout = self.timeout if timeout <= 0: timeout = None msg = self.kc.shell_channel.get_msg(timeout=timeout) except Empty: self.log.error("""Timeout waiting for execute reply (%is). If your cell should take longer than this, you can increase the timeout with: c.ExecutePreprocessor.timeout = SECONDS in jupyter_nbconvert_config.py """ % self.timeout) if self.interrupt_on_timeout: self.log.error("Interrupting kernel") self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception("Cell execution timed out, see log" " for details.") if msg['parent_header'].get('msg_id') == msg_id: break else: # not our reply continue outs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(timeout=4) except Empty: self.log.warn("Timeout waiting for IOPub output") break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
def run_cell(self, cell): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) # wait for finish, with timeout while True: try: msg = self.kc.shell_channel.get_msg(timeout=self.timeout) except Empty: self.log.error("""Timeout waiting for execute reply (%is). If your cell should take longer than this, you can increase the timeout with: c.ExecutePreprocessor.timeout = SECONDS in jupyter_nbconvert_config.py """ % self.timeout) if self.interrupt_on_timeout: self.log.error("Interrupting kernel") self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception("Cell execution timed out, see log" " for details.") if msg['parent_header'].get('msg_id') == msg_id: break else: # not our reply continue outs = [] while True: try: msg = self.kc.iopub_channel.get_msg(timeout=self.timeout) except Empty: self.log.warn("Timeout waiting for IOPub output") break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
def run_cell(self, cell): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) # wait for finish, with timeout while True: try: if self.timeout_func is not None: timeout = self.timeout_func(cell) else: timeout = self.timeout if not timeout or timeout < 0: timeout = None msg = self.kc.shell_channel.get_msg(timeout=timeout) except Empty: self.log.error("Timeout waiting for execute reply (%is)." % self.timeout) if self.interrupt_on_timeout: self.log.error("Interrupting kernel") self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception("Cell execution timed out") if msg['parent_header'].get('msg_id') == msg_id: break else: # not our reply continue outs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(timeout=4) except Empty: self.log.warn("Timeout waiting for IOPub output") if self.raise_on_iopub_timeout: raise RuntimeError("Timeout waiting for IOPub output") else: break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
async def async_execute_cell(self, cell, cell_index=0, execution_count=None, store_history=True): # sos is the additional meta information sent to kernel if cell.cell_type != 'code' or not cell.source.strip(): self.log.debug("Skipping non-executing cell %s", cell_index) return cell if self.record_timing and 'execution' not in cell['metadata']: cell['metadata']['execution'] = {} sos_meta = self._prepare_meta(cell) content = dict(code=cell.source, silent=False, store_history=store_history, user_expressions='', allow_stdin=False, stop_on_error=False, sos=sos_meta) msg = self.kc.session.msg('execute_request', content) self.kc.shell_channel.send(msg) msg_id = msg['header']['msg_id'] # the reset is copied from https://github.com/jupyter/nbconvert/blob/master/nbconvert/preprocessors/execute.py # because we only need to change the first line # msg_id = self.kc.execute(cell.source) self.log.debug( f"Executing cell {cell_index} with kernel {content['sos']['cell_kernel']}:\n{cell.source}" ) exec_reply = await self.async_wait_for_reply(msg_id, cell) self.code_cells_executed += 1 exec_timeout = self._get_timeout(cell) cell.outputs = [] self.clear_before_next_output = False while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = await ensure_async( self.kc.iopub_channel.get_msg(timeout=self.iopub_timeout)) except Empty: self.log.warning("Timeout waiting for IOPub output") if self.raise_on_iopub_timeout: raise RuntimeError("Timeout waiting for IOPub output") else: break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in exec_reply['content']: cell['execution_count'] = exec_reply['content'][ 'execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': cell.outputs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if cell_index in cell_map: cell_map[cell_index] = [] continue elif msg_type.startswith('comm'): continue display_id = None if msg_type in { 'execute_result', 'display_data', 'update_display_data' }: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) continue if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(cell.outputs)) cell.outputs.append(out) return cell
def run_cell(self, cell, cell_index=0): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) exec_reply = self._wait_for_reply(msg_id, cell) outs = cell.outputs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(timeout=self.iopub_timeout) except Empty: self.log.warn("Timeout waiting for IOPub output") if self.raise_on_iopub_timeout: raise RuntimeError("Timeout waiting for IOPub output") else: break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if cell_index in cell_map: cell_map[cell_index] = [] continue elif msg_type.startswith('comm'): continue display_id = None if msg_type in {'execute_result', 'display_data', 'update_display_data'}: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) continue if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) outs.append(out) return exec_reply, outs
def run_cell(self, src): cell = {} cell["source"] = src.lstrip() msg_id = self.kc.execute(src.lstrip(), store_history=False) # wait for finish, with timeout while True: try: timeout = self.timeout if timeout < 0: timeout = None msg = self.kc.get_shell_msg(timeout=timeout) except Empty: if self.interrupt_on_timeout: self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception( "Cell execution timed out, see log for details.") if msg['parent_header'].get('msg_id') == msg_id: break else: # not our reply continue outs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(block=True, timeout=4) except Empty: print("Timeout waiting for IOPub output\nTry restarting python session and running weave again") raise RuntimeError("Timeout waiting for IOPub output") #stdout from InProcessKernelManager has no parent_header if msg['parent_header'].get('msg_id') != msg_id and msg['msg_type'] != "stream": continue msg_type = msg['msg_type'] content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: print("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
def run_cell(self, cell): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) # wait for finish, with timeout while True: try: msg = self.kc.shell_channel.get_msg(timeout=self.timeout) except Empty: self.log.error("""Timeout waiting for execute reply (%is). If your cell should take longer than this, you can increase the timeout with: c.ExecutePreprocessor.timeout = SECONDS in jupyter_nbconvert_config.py """ % self.timeout) if self.interrupt_on_timeout: self.log.error("Interrupting kernel") self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception("Cell execution timed out, see log" " for details.") if msg['parent_header'].get('msg_id') == msg_id: if msg['content']['status'] == 'error' and not self.allow_errors: raise CellExecutionError(msg['content']['traceback']) else: break else: # not our reply continue outs = [] while True: try: msg = self.kc.iopub_channel.get_msg(timeout=self.timeout) except Empty: self.log.warn("Timeout waiting for IOPub output") break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
def run_cell(self, source): """Execute the source. Return a list of :class:`nbformat.NotebookNode` instance. """ source = source.lstrip() cell = {} cell['source'] = source message_id = self._kernel_client.execute(source, store_history=False) # self._logger.debug('message_id {}'.format(message_id)) self._wait_for_finish(message_id) outputs = JupyterOutputs() while True: try: # We've already waited for execute_reply, so all output should already be # waiting. However, on slow networks, like in certain CI systems, waiting < 1 second # might miss messages. So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. message = self._kernel_client.iopub_channel.get_msg(block=True, timeout=4) # self._logger.debug('message {}'.format(message)) except Empty: message = 'Timeout waiting for IOPub output' self._logger.error(message) # \nTry restarting python session and running again raise TimeoutError(message) # stdout from InProcessKernelManager has no parent_header if not self.message_id_match( message, message_id) and message['msg_type'] != 'stream': continue message_type = message['msg_type'] content = message['content'] # self._logger.debug('msg_type {}'.format(message_type)) # self._logger.debug('content {}'.format(content)) # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if message_type == 'status': if content['execution_state'] == 'idle': break # exit while loop else: continue elif message_type == 'execute_input': continue elif message_type == 'clear_output': outputs = JupyterOutputs() continue elif message_type.startswith('comm'): continue try: output = nbformat_v4.output_from_msg(message) except ValueError: self._logger.error( 'unhandled iopub message: {}'.format(message_type)) else: outputs.append(JupyterOutput(output)) return outputs
def run_cell(self, src): cell = {} cell["source"] = src.lstrip() msg_id = self.kc.execute(src.lstrip(), store_history=False) # wait for finish, with timeout while True: try: timeout = self.timeout if timeout < 0: timeout = None msg = self.kc.get_shell_msg(timeout=timeout) except Empty: if self.interrupt_on_timeout: self.km.interrupt_kernel() break else: try: exception = TimeoutError except NameError: exception = RuntimeError raise exception( "Cell execution timed out, see log for details.") if msg['parent_header'].get('msg_id') == msg_id: break else: # not our reply continue outs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(block=True, timeout=4) except Empty: print( "Timeout waiting for IOPub output\nTry restarting python session and running weave again" ) raise RuntimeError("Timeout waiting for IOPub output") #stdout from InProcessKernelManager has no parent_header if msg['parent_header'].get( 'msg_id') != msg_id and msg['msg_type'] != "stream": continue msg_type = msg['msg_type'] content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs = [] continue elif msg_type.startswith('comm'): continue try: out = output_from_msg(msg) except ValueError: print("unhandled iopub msg: " + msg_type) else: outs.append(out) return outs
def run_cell(self, cell, cell_index=0): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) outs = cell.outputs = [] while True: try: # We are not waiting for execute_reply, so all output # will not be waiting for us. This may produce currently unknown issues. msg = self.kc.iopub_channel.get_msg(timeout=None) except Empty: self.log.warning("Timeout waiting for IOPub output") if self.raise_on_iopub_timeout: raise RuntimeError("Timeout waiting for IOPub output") else: break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': if self.log_output: sys.stdout.write( 'Executing Cell {:-<40}\n'.format(content.get("execution_count", "*"))) continue elif msg_type == 'clear_output': outs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if cell_index in cell_map: cell_map[cell_index] = [] continue elif msg_type.startswith('comm'): continue display_id = None if msg_type in {'execute_result', 'display_data', 'update_display_data'}: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) continue if display_id: cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) if self.log_output: log_output(out) outs.append(out) exec_reply = self._wait_for_reply(msg_id, cell) if self.log_output: sys.stdout.write('Ending Cell {:-<43}\n'.format( exec_reply.get("content",{}).get("execution_count", content))) return exec_reply, outs
def run_cell(self, cell, cell_index=0): msg_id = self.kc.execute(cell.source) self.log.debug("Executing cell:\n%s", cell.source) exec_reply = self._wait_for_reply(msg_id, cell) outs = cell.outputs = [] while True: try: # We've already waited for execute_reply, so all output # should already be waiting. However, on slow networks, like # in certain CI systems, waiting < 1 second might miss messages. # So long as the kernel sends a status:idle message when it # finishes, we won't actually have to wait this long, anyway. msg = self.kc.iopub_channel.get_msg(timeout=self.iopub_timeout) except Empty: self.log.warning("Timeout waiting for IOPub output") if self.raise_on_iopub_timeout: raise RuntimeError("Timeout waiting for IOPub output") else: break if msg['parent_header'].get('msg_id') != msg_id: # not an output from our execution continue msg_type = msg['msg_type'] self.log.debug("output: %s", msg_type) content = msg['content'] # set the prompt number for the input and the output if 'execution_count' in content: cell['execution_count'] = content['execution_count'] if msg_type == 'status': if content['execution_state'] == 'idle': break else: continue elif msg_type == 'execute_input': continue elif msg_type == 'clear_output': outs[:] = [] # clear display_id mapping for this cell for display_id, cell_map in self._display_id_map.items(): if cell_index in cell_map: cell_map[cell_index] = [] continue elif msg_type.startswith('comm'): continue display_id = None if msg_type in {'execute_result', 'display_data', 'update_display_data'}: display_id = msg['content'].get('transient', {}).get('display_id', None) if display_id: self._update_display_id(display_id, msg) if msg_type == 'update_display_data': # update_display_data doesn't get recorded continue try: out = output_from_msg(msg) except ValueError: self.log.error("unhandled iopub msg: " + msg_type) continue if display_id: # record output index in: # _display_id_map[display_id][cell_idx] cell_map = self._display_id_map.setdefault(display_id, {}) output_idx_list = cell_map.setdefault(cell_index, []) output_idx_list.append(len(outs)) outs.append(out) return exec_reply, outs