logging.error(merr) else: with codecs.open(self._path, 'w', 'utf8') as fp: write(nr.nb, fp, IPYNB_VER) # clear surplus info first_cell = nr.nb['cells'][0] first_cell['outputs'] = [] del nr.nb['cells'][1:] last_run = datetime.fromtimestamp(time.time()) last_run = convert_server_time_to_client(last_run) last_run = last_run.strftime("%Y-%m-%d %H:%M:%S") body = [' "last_run": "{}"'.format(last_run)] body.append(' "elapsed": "{}"'.format(elapsed)) body.append(' "max_memory": "{}"'.format(sizeof_fmt(max_mem))) body.append(' "error": {}'.format('None' if err is None else json.dumps(err))) if self._dep_files_chksum is not None or\ self._dep_hdf_chksum is not None: self._write_result_depends(body) if self._out_hdf_chksum is not None: # could be multiple outputs coutput = ' "hdf": {}'.format(self._out_hdf_chksum) body.append(' "output": {{\n{}\n }}'.format(coutput)) if len(body) > 0: newcell = copy.deepcopy(nr.nb['cells'][0]) cs_body = "# WARNING: Generated results. Do Not Edit.\n{{\n{}\n}}".\ format(',\n'.join(body)) newcell['source'] = cs_body
def hsize(self): """Return human readable total size of all files.""" _size = 0 for _file in self.files: _size += _file.size return sizeof_fmt(_size)
def run_cell(self, cell, cidx): ''' Run a notebook cell and update the output of that cell in-place. ''' logging.debug('running cell {}'.format(cidx)) # logging.debug(u'cell.input {}'.format(cell.input)) self.kc.execute(cell.source) reply = self.kc.get_shell_msg() status = reply['content']['status'] max_mem = system_memory_used() logging.info(' memory used: {}'.format(sizeof_fmt(max_mem))) if status == 'error': traceback_text = 'Cell raised uncaught exception: \n' + \ '\n'.join(reply['content']['traceback']) traceback_text = remove_ansicolor(traceback_text) if 'NoDataFound' not in traceback_text: logging.error(traceback_text) else: logging.debug('run_cell ok') outs = list() while True: try: msg = self.kc.get_iopub_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.error("empty exception") raise content = msg['content'] msg_type = msg['msg_type'] # IPython 3.0.0-dev writes pyerr/pyout in the notebook format but # uses error/execute_result in the message spec. This does the # translation needed for tests to pass with IPython 3.0.0-dev notebook3_format_conversions = { 'error': 'pyerr', 'execute_result': 'pyout' } msg_type = notebook3_format_conversions.get(msg_type, 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', 'execute_input'): continue elif msg_type == 'stream': out.stream = content['name'] if 'text' in content: out.text = content['text'] else: out.text = content['data'] # print(out.text, end='') elif msg_type in ('display_data', 'pyout'): for mime, data in content['data'].items(): try: attr = self.MIME_MAP[mime] except KeyError: logging.error("unhandled mime") raise NotImplementedError('unhandled mime type: %s' % mime) setattr(out, attr, data) elif msg_type == 'pyerr': out.ename = content['ename'] out.evalue = content['evalue'] out.traceback = content['traceback'] elif msg_type == 'clear_output': outs = list() continue else: logging.error("unhandled iopub") raise NotImplementedError('unhandled iopub message: %s' % msg_type) outs.append(out) # NOTE: Ver 4 format still have 'pyout', Why? cell['outputs'] = upgrade_outputs(outs) logging.debug("status: {}".format(status)) if status == 'error': if 'NoDataFound' in traceback_text: raise NoDataFound(traceback_text.split('\n')[-1]) else: logging.debug(u"NotebookError raised") raise NotebookError(traceback_text)
def hsize(self): """Return human readable file size.""" _size = os.path.getsize(self.abspath) return sizeof_fmt(_size)