def poll_for_msgs(self): """Polls for messages from the kernel. Used after submitting code for execution""" try: msg = self.iopub.get_msg(timeout=1) if msg['msg_type'] == 'status' and msg['content'][ 'execution_state'] == 'idle': if _debugging: logging.info('Message -- {}:{}'.format( msg['msg_type'], msg['content'])) self._previous_status = 'IDLE' return NotebookNode(output_type='IDLE') except Empty: # state should return to idle before queue becomes empty, but we ignore it now prevstat, self._previous_status = self._previous_status, 'EMPTY' retstat = 'END_CELL' if prevstat == 'IDLE' else 'EMPTY' # Assuming IDLE followed by EMPTY is the end-of-cell return NotebookNode(output_type=retstat) self._previous_status = '' # Not idle, that's all we are concerned about for now content, msg_type = msg['content'], msg['msg_type'] if msg_type in ['status', 'pyin']: return NotebookNode(output_type='NoOp') out = NotebookNode(output_type=msg_type) if msg_type in ('display_data', 'pyout'): for mime, data in content['data'].items(): try: attr = self.MIME_MAP[mime] tmpval = RClansiconv(data) if attr == 'text' else data setattr(out, attr, tmpval) except KeyError: raise NotImplementedError('unhandled mime type: %s' % mime) elif msg_type == 'stream': setattr(out, 'text', RClansiconv(content['data'])) elif msg_type == 'pyerr': setattr(out, 'html', RClansiconv('\n'.join(content['traceback']) + '\n')) else: if _debugging: logging.info('Unsupported: ' + msg_type) raise NotImplementedError('unhandled iopub message: %s' % msg_type) if _debugging: logging.info( 'Sending: msg_type: [{}]; HTML: [{}]; TEXT: [{}]'.format( msg_type, out.get('html', ''), out.get('text', ''))) return out # upstream process will handle it [e.g. send as an oob message]
def poll_for_msgs(self): """Polls for messages from the kernel. Used after submitting code for execution""" try: msg = self.iopub.get_msg(timeout=1) if msg['msg_type'] == 'status' and msg['content']['execution_state'] == 'idle': if _debugging: logging.info('Message -- {}:{}'.format(msg['msg_type'], msg['content'])) self._previous_status = 'IDLE' return NotebookNode(output_type = 'IDLE') except Empty: # state should return to idle before queue becomes empty, but we ignore it now prevstat, self._previous_status = self._previous_status, 'EMPTY' retstat = 'END_CELL' if prevstat == 'IDLE' else 'EMPTY' # Assuming IDLE followed by EMPTY is the end-of-cell return NotebookNode(output_type = retstat) self._previous_status = '' # Not idle, that's all we are concerned about for now content, msg_type = msg['content'], msg['msg_type'] if msg_type in ['status', 'pyin']: return NotebookNode(output_type = 'NoOp') out = NotebookNode(output_type = msg_type) if msg_type in ('display_data', 'pyout'): for mime, data in content['data'].items(): try: attr = self.MIME_MAP[mime] tmpval = RClansiconv(data) if attr == 'text' else data setattr(out, attr, tmpval) except KeyError: raise NotImplementedError('unhandled mime type: %s' % mime) elif msg_type == 'stream': setattr(out, 'text', RClansiconv(content['data'])) elif msg_type == 'pyerr': setattr(out, 'html', RClansiconv('\n'.join(content['traceback']) + '\n')) else: if _debugging: logging.info('Unsupported: ' + msg_type) raise NotImplementedError('unhandled iopub message: %s' % msg_type) if _debugging: logging.info('Sending: msg_type: [{}]; HTML: [{}]; TEXT: [{}]'.format(msg_type, out.get('html', ''), out.get('text', '') )) return out # upstream process will handle it [e.g. send as an oob message]