Пример #1
0
    def run_cell(self, cell):
        '''
        Run a notebook cell and update the output of that cell in-place.
        '''
        if _debugging: logging.debug('Running cell:\n%s\n', cell.input)
        self.shell.execute(cell.input)
        reply = self.shell.get_msg()
        status = reply['content']['status']
        if status == 'error':
            logging.info('Cell raised uncaught exception: \n%s', '\n'.join(reply['content']['traceback']))
        else:
            if _debugging: logging.debug('Cell returned')

        outs = list()
        while True:
            try:
                msg = self.iopub.get_msg(timeout=1)
                if msg['msg_type'] == 'status':
                    if msg['content']['execution_state'] == 'idle':
                        break
            except Empty:
                # execution state should return to idle before the queue becomes empty,
                # if it doesn't, something bad has happened
                logging.warn('Execution state did not return to idle')
                raise

            content = msg['content']
            msg_type = msg['msg_type']

            out = NotebookNode(output_type=msg_type)

            if 'execution_count' in content:
                cell['prompt_number'] = content['execution_count']
                out.prompt_number = content['execution_count']

            if msg_type in ['status', 'pyin']:
                continue
            elif msg_type == 'stream':
                out.stream = content['name']
                out.html = RClansiconv(content['data'])
            elif msg_type in ('display_data', 'pyout'):
                for mime, data in content['data'].items():
                    try:
                        attr = self.MIME_MAP[mime]
                    except KeyError:
                        logging.warning('unhandled mime type: %s' % mime)
                        raise NotImplementedError('unhandled mime type: %s' % mime)
                    if attr == 'text':
                        if _debugging: logging.info(data)
                        setattr(out, attr, RClansiconv(data))
                    else:
                        setattr(out, attr, data)
            elif msg_type == 'pyerr':
                logging.info('Received an exception: ' + content['ename'])
                out.ename = content['ename']
                out.evalue = content['evalue']
                out.traceback = content['traceback']
                out.html = RClansiconv('\n'.join(out.traceback))
            else:
                raise NotImplementedError('unhandled iopub message: %s' % msg_type)
            outs.append(out)
        cell['outputs'] = outs
Пример #2
0
    def run_cell(self, cell):
        '''
        Run a notebook cell and update the output of that cell in-place.
        '''
        if _debugging: logging.debug('Running cell:\n%s\n', cell.input)
        self.shell.execute(cell.input)
        reply = self.shell.get_msg()
        status = reply['content']['status']
        if status == 'error':
            logging.info('Cell raised uncaught exception: \n%s',
                         '\n'.join(reply['content']['traceback']))
        else:
            if _debugging: logging.debug('Cell returned')

        outs = list()
        while True:
            try:
                msg = self.iopub.get_msg(timeout=1)
                if msg['msg_type'] == 'status':
                    if msg['content']['execution_state'] == 'idle':
                        break
            except Empty:
                # execution state should return to idle before the queue becomes empty,
                # if it doesn't, something bad has happened
                # logging.warn('Execution state did not return to idle')
                pass  # raise

            content = msg['content']
            msg_type = msg['msg_type']

            out = NotebookNode(output_type=msg_type)

            if 'execution_count' in content:
                cell['prompt_number'] = content['execution_count']
                out.prompt_number = content['execution_count']

            if msg_type in ['status', 'pyin']:
                continue
            elif msg_type == 'stream':
                out.stream = content['name']
                out.html = RClansiconv(content['data'])
            elif msg_type in ('display_data', 'pyout'):
                for mime, data in content['data'].items():
                    try:
                        attr = self.MIME_MAP[mime]
                    except KeyError:
                        logging.warning('unhandled mime type: %s' % mime)
                        raise NotImplementedError('unhandled mime type: %s' %
                                                  mime)
                    if attr == 'text':
                        if _debugging: logging.info(data)
                        setattr(out, attr, RClansiconv(data))
                    else:
                        setattr(out, attr, data)
            elif msg_type == 'pyerr':
                logging.info('Received an exception: ' + content['ename'])
                out.ename = content['ename']
                out.evalue = content['evalue']
                out.traceback = content['traceback']
                out.html = RClansiconv('\n'.join(out.traceback))
            else:
                raise NotImplementedError('unhandled iopub message: %s' %
                                          msg_type)
            outs.append(out)
        cell['outputs'] = outs