Permalink
Browse files

Adding tracebacks, evalue and etype to the nbformat and notebook.

  • Loading branch information...
1 parent 7debcc4 commit 366996236fa458226806dd948ffc7c7983ce9373 @ellisonbg ellisonbg committed Aug 13, 2011
@@ -166,36 +166,21 @@ def delete(self, notebook_id):
# RST web service handlers
#-----------------------------------------------------------------------------
-_rst_header = """========
-Heading1
-========
-
-Heading2
-========
-
-Heading3
---------
-
-Heading4
-^^^^^^^^
-
-"""
class RSTHandler(web.RequestHandler):
def post(self):
if publish_string is None:
raise web.HTTPError(503)
body = self.request.body.strip()
- source = _rst_header + body
- template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html')
+ source = body
+ # template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html')
print template_path
defaults = {'file_insertion_enabled': 0,
'raw_enabled': 0,
'_disable_config': 1,
'stylesheet_path': 0,
- 'initial_header_level': 3,
- 'template': template_path
+ # 'template': template_path
}
try:
html = publish_string(source, writer_name='html',
@@ -204,8 +189,6 @@ def post(self):
except:
raise web.HTTPError(400)
print html
-# html = '\n'.join(html.split('\n')[7:-3])
-# print html
self.set_header('Content-Type', 'text/html')
self.finish(html)
@@ -204,7 +204,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.append_pyerr = function (json) {
var tb = json.traceback;
- if (tb !== undefined) {
+ if (tb !== undefined && tb.length > 0) {
var s = '';
var len = tb.length;
for (var i=0; i<len; i++) {
@@ -262,7 +262,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.append_text = function (data, element) {
element = element || this.element.find("div.output");
var toinsert = $("<div/>").addClass("output_stream");
- toinsert.append($("<pre/>").html(utils.fixConsole(data)));
+ toinsert.append($("<pre/>").html(data));
element.append(toinsert);
return element;
};
@@ -520,7 +520,7 @@ var IPython = (function (IPython) {
var json = {};
json.output_type = msg_type;
if (msg_type === "stream") {
- json.text = content.data + '\n';
+ json.text = utils.fixConsole(content.data + '\n');
} else if (msg_type === "display_data") {
json = this.convert_mime_types(json, content.data);
} else if (msg_type === "pyout") {
@@ -529,15 +529,19 @@ var IPython = (function (IPython) {
} else if (msg_type === "pyerr") {
json.ename = content.ename;
json.evalue = content.evalue;
- json.traceback = content.traceback;
+ var traceback = [];
+ for (var i=0; i<content.traceback.length; i++) {
+ traceback.push(utils.fixConsole(content.traceback[i]));
+ }
+ json.traceback = traceback;
};
cell.append_output(json);
};
Notebook.prototype.convert_mime_types = function (json, data) {
if (data['text/plain'] !== undefined) {
- json.text = data['text/plain'];
+ json.text = utils.fixConsole(data['text/plain']);
};
if (data['text/html'] !== undefined) {
json.html = data['text/html'];
@@ -688,7 +692,6 @@ var IPython = (function (IPython) {
Notebook.prototype.notebook_saved = function (data, status, xhr) {
setTimeout($.proxy(IPython.save_widget.status_save,IPython.save_widget),500);
-// IPython.save_widget.status_save();
}
@@ -24,29 +24,43 @@ def from_dict(d):
def new_output(output_type=None, output_text=None, output_png=None,
output_html=None, output_svg=None, output_latex=None, output_json=None,
- output_javascript=None, output_jpeg=None, prompt_number=None):
+ output_javascript=None, output_jpeg=None, prompt_number=None,
+ etype=None, evalue=None, traceback=None):
"""Create a new code cell with input and output"""
output = NotebookNode()
if output_type is not None:
output.output_type = unicode(output_type)
- if output_text is not None:
- output.text = unicode(output_text)
- if output_png is not None:
- output.png = bytes(output_png)
- if output_jpeg is not None:
- output.jpeg = bytes(output_jpeg)
- if output_html is not None:
- output.html = unicode(output_html)
- if output_svg is not None:
- output.svg = unicode(output_svg)
- if output_latex is not None:
- output.latex = unicode(output_latex)
- if output_json is not None:
- output.json = unicode(output_json)
- if output_javascript is not None:
- output.javascript = unicode(output_javascript)
- if prompt_number is not None:
- output.prompt_number = int(prompt_number)
+
+ if output_type != 'pyerr':
+ if output_text is not None:
+ output.text = unicode(output_text)
+ if output_png is not None:
+ output.png = bytes(output_png)
+ if output_jpeg is not None:
+ output.jpeg = bytes(output_jpeg)
+ if output_html is not None:
+ output.html = unicode(output_html)
+ if output_svg is not None:
+ output.svg = unicode(output_svg)
+ if output_latex is not None:
+ output.latex = unicode(output_latex)
+ if output_json is not None:
+ output.json = unicode(output_json)
+ if output_javascript is not None:
+ output.javascript = unicode(output_javascript)
+
+ if output_type == u'pyout':
+ if prompt_number is not None:
+ output.prompt_number = int(prompt_number)
+
+ if output_type == u'pyerr':
+ if etype is not None:
+ output.etype = unicode(etype)
+ if evalue is not None:
+ output.evalue = unicode(evalue)
+ if traceback is not None:
+ output.traceback = [unicode(frame) for frame in list(traceback)]
+
return output
@@ -90,53 +90,64 @@ def reads(self, s, **kwargs):
return self.to_notebook(root, **kwargs)
def to_notebook(self, root, **kwargs):
- nbname = _get_text(root,'name')
- nbid = _get_text(root,'id')
- nbauthor = _get_text(root,'author')
- nbemail = _get_text(root,'email')
- nblicense = _get_text(root,'license')
- nbcreated = _get_text(root,'created')
- nbsaved = _get_text(root,'saved')
+ nbname = _get_text(root,u'name')
+ nbid = _get_text(root,u'id')
+ nbauthor = _get_text(root,u'author')
+ nbemail = _get_text(root,u'email')
+ nblicense = _get_text(root,u'license')
+ nbcreated = _get_text(root,u'created')
+ nbsaved = _get_text(root,u'saved')
worksheets = []
- for ws_e in root.find('worksheets').getiterator('worksheet'):
- wsname = _get_text(ws_e,'name')
+ for ws_e in root.find(u'worksheets').getiterator(u'worksheet'):
+ wsname = _get_text(ws_e,u'name')
cells = []
- for cell_e in ws_e.find('cells').getiterator():
- if cell_e.tag == 'codecell':
- input = _get_text(cell_e,'input')
- prompt_number = _get_int(cell_e,'prompt_number')
- collapsed = _get_bool(cell_e,'collapsed')
- language = _get_text(cell_e,'language')
+ for cell_e in ws_e.find(u'cells').getiterator():
+ if cell_e.tag == u'codecell':
+ input = _get_text(cell_e,u'input')
+ prompt_number = _get_int(cell_e,u'prompt_number')
+ collapsed = _get_bool(cell_e,u'collapsed')
+ language = _get_text(cell_e,u'language')
outputs = []
- for output_e in cell_e.find('outputs').getiterator('output'):
- out_prompt_number = _get_int(output_e,'prompt_number')
- output_type = _get_text(output_e,'output_type')
- output_text = _get_text(output_e,'text')
- output_png = _get_binary(output_e,'png')
- output_jpeg = _get_binary(output_e,'jpeg')
- output_svg = _get_text(output_e,'svg')
- output_html = _get_text(output_e,'html')
- output_latex = _get_text(output_e,'latex')
- output_json = _get_text(output_e,'json')
- output_javascript = _get_text(output_e,'javascript')
+ for output_e in cell_e.find(u'outputs').getiterator(u'output'):
+ output_type = _get_text(output_e,u'output_type')
+ output_text = _get_text(output_e,u'text')
+ output_png = _get_binary(output_e,u'png')
+ output_jpeg = _get_binary(output_e,u'jpeg')
+ output_svg = _get_text(output_e,u'svg')
+ output_html = _get_text(output_e,u'html')
+ output_latex = _get_text(output_e,u'latex')
+ output_json = _get_text(output_e,u'json')
+ output_javascript = _get_text(output_e,u'javascript')
+
+ out_prompt_number = _get_int(output_e,u'prompt_number')
+ etype = _get_text(output_e,u'etype')
+ evalue = _get_text(output_e,u'evalue')
+ traceback = []
+ traceback_e = output_e.find(u'traceback')
+ if traceback_e is not None:
+ for frame_e in traceback_e.getiterator(u'frame'):
+ traceback.append(frame_e.text)
+ if len(traceback) == 0:
+ traceback = None
output = new_output(output_type=output_type,output_png=output_png,
output_text=output_text, output_svg=output_svg,
output_html=output_html, output_latex=output_latex,
output_json=output_json, output_javascript=output_javascript,
- output_jpeg=output_jpeg, prompt_number=out_prompt_number
+ output_jpeg=output_jpeg, prompt_number=out_prompt_number,
+ etype=etype, evalue=evalue, traceback=traceback
)
outputs.append(output)
cc = new_code_cell(input=input,prompt_number=prompt_number,
language=language,outputs=outputs,collapsed=collapsed)
cells.append(cc)
- if cell_e.tag == 'htmlcell':
- source = _get_text(cell_e,'source')
- rendered = _get_text(cell_e,'rendered')
+ if cell_e.tag == u'htmlcell':
+ source = _get_text(cell_e,u'source')
+ rendered = _get_text(cell_e,u'rendered')
cells.append(new_text_cell(u'html', source=source, rendered=rendered))
- if cell_e.tag == 'markdowncell':
- source = _get_text(cell_e,'source')
- rendered = _get_text(cell_e,'rendered')
+ if cell_e.tag == u'markdowncell':
+ source = _get_text(cell_e,u'source')
+ rendered = _get_text(cell_e,u'rendered')
cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
ws = new_worksheet(name=wsname,cells=cells)
worksheets.append(ws)
@@ -149,49 +160,56 @@ def to_notebook(self, root, **kwargs):
class XMLWriter(NotebookWriter):
def writes(self, nb, **kwargs):
- nb_e = ET.Element('notebook')
- _set_text(nb,'name',nb_e,'name')
- _set_text(nb,'id',nb_e,'id')
- _set_text(nb,'author',nb_e,'author')
- _set_text(nb,'email',nb_e,'email')
- _set_text(nb,'license',nb_e,'license')
- _set_text(nb,'created',nb_e,'created')
- _set_text(nb,'saved',nb_e,'saved')
- _set_int(nb,'nbformat',nb_e,'nbformat')
- wss_e = ET.SubElement(nb_e,'worksheets')
+ nb_e = ET.Element(u'notebook')
+ _set_text(nb,u'name',nb_e,u'name')
+ _set_text(nb,u'id',nb_e,u'id')
+ _set_text(nb,u'author',nb_e,u'author')
+ _set_text(nb,u'email',nb_e,u'email')
+ _set_text(nb,u'license',nb_e,u'license')
+ _set_text(nb,u'created',nb_e,u'created')
+ _set_text(nb,u'saved',nb_e,u'saved')
+ _set_int(nb,u'nbformat',nb_e,u'nbformat')
+ wss_e = ET.SubElement(nb_e,u'worksheets')
for ws in nb.worksheets:
- ws_e = ET.SubElement(wss_e, 'worksheet')
- _set_text(ws,'name',ws_e,'name')
- cells_e = ET.SubElement(ws_e,'cells')
+ ws_e = ET.SubElement(wss_e, u'worksheet')
+ _set_text(ws,u'name',ws_e,u'name')
+ cells_e = ET.SubElement(ws_e,u'cells')
for cell in ws.cells:
cell_type = cell.cell_type
- if cell_type == 'code':
- cell_e = ET.SubElement(cells_e, 'codecell')
- _set_text(cell,'input',cell_e,'input')
- _set_text(cell,'language',cell_e,'language')
- _set_int(cell,'prompt_number',cell_e,'prompt_number')
- _set_bool(cell,'collapsed',cell_e,'collapsed')
- outputs_e = ET.SubElement(cell_e, 'outputs')
+ if cell_type == u'code':
+ cell_e = ET.SubElement(cells_e, u'codecell')
+ _set_text(cell,u'input',cell_e,u'input')
+ _set_text(cell,u'language',cell_e,u'language')
+ _set_int(cell,u'prompt_number',cell_e,u'prompt_number')
+ _set_bool(cell,u'collapsed',cell_e,u'collapsed')
+ outputs_e = ET.SubElement(cell_e, u'outputs')
for output in cell.outputs:
- output_e = ET.SubElement(outputs_e, 'output')
- _set_int(output,'prompt_number',output_e,'prompt_number')
- _set_text(output,'output_type',output_e,'output_type')
- _set_text(output,'text',output_e,'text')
- _set_binary(output,'png',output_e,'png')
- _set_binary(output,'jpeg',output_e,'jpeg')
- _set_text(output,'html',output_e,'html')
- _set_text(output,'svg',output_e,'svg')
- _set_text(output,'latex',output_e,'latex')
- _set_text(output,'json',output_e,'json')
- _set_text(output,'javascript',output_e,'javascript')
- elif cell_type == 'html':
- cell_e = ET.SubElement(cells_e, 'htmlcell')
- _set_text(cell,'source',cell_e,'source')
- _set_text(cell,'rendered',cell_e,'rendered')
- elif cell_type == 'markdown':
- cell_e = ET.SubElement(cells_e, 'markdowncell')
- _set_text(cell,'source',cell_e,'source')
- _set_text(cell,'rendered',cell_e,'rendered')
+ output_e = ET.SubElement(outputs_e, u'output')
+ _set_text(output,u'output_type',output_e,u'output_type')
+ _set_text(output,u'text',output_e,u'text')
+ _set_binary(output,u'png',output_e,u'png')
+ _set_binary(output,u'jpeg',output_e,u'jpeg')
+ _set_text(output,u'html',output_e,u'html')
+ _set_text(output,u'svg',output_e,u'svg')
+ _set_text(output,u'latex',output_e,u'latex')
+ _set_text(output,u'json',output_e,u'json')
+ _set_text(output,u'javascript',output_e,u'javascript')
+ _set_int(output,u'prompt_number',output_e,u'prompt_number')
+ _set_text(output,u'etype',output_e,u'etype')
+ _set_text(output,u'evalue',output_e,u'evalue')
+ if u'traceback' in output:
+ tb_e = ET.SubElement(output_e, u'traceback')
+ for frame in output.traceback:
+ frame_e = ET.SubElement(tb_e, u'frame')
+ frame_e.text = frame
+ elif cell_type == u'html':
+ cell_e = ET.SubElement(cells_e, u'htmlcell')
+ _set_text(cell,u'source',cell_e,u'source')
+ _set_text(cell,u'rendered',cell_e,u'rendered')
+ elif cell_type == u'markdown':
+ cell_e = ET.SubElement(cells_e, u'markdowncell')
+ _set_text(cell,u'source',cell_e,u'source')
+ _set_text(cell,u'rendered',cell_e,u'rendered')
indent(nb_e)
txt = ET.tostring(nb_e, encoding="utf-8")
@@ -56,16 +56,20 @@
output_jpeg=b'data',
output_svg=u'<svg>',
output_json=u'json data',
- output_javascript=u'var i=0;',
- prompt_number=4
+ output_javascript=u'var i=0;'
+ ),new_output(
+ output_type=u'pyerr',
+ etype=u'NameError',
+ evalue=u'NameError was here',
+ traceback=[u'frame 0', u'frame 1', u'frame 2']
)]
))
nb0 = new_notebook(
name='nb0',
worksheets=[ws, new_worksheet(name='worksheet2')],
author='Bart Simpson',
- email='bsimple@fox.com',
+ email='bsimpson@fox.com',
saved='ISO8601_goes_here',
created='ISO8601_goes_here',
license='BSD'
Oops, something went wrong.

0 comments on commit 3669962

Please sign in to comment.