Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added saving and loading of output of all types.

  • Loading branch information...
commit 2a85499c7b0afab9f21bb6b78873c75231974569 1 parent 7640e54
@ellisonbg ellisonbg authored
View
79 IPython/frontend/html/notebook/static/js/codecell.js
@@ -12,6 +12,7 @@ var IPython = (function (IPython) {
this.input_prompt_number = ' ';
this.is_completing = false;
this.completion_cursor = null;
+ this.outputs = [];
IPython.Cell.apply(this, arguments);
};
@@ -147,50 +148,77 @@ var IPython = (function (IPython) {
};
- CodeCell.prototype.append_pyout = function (data, n) {
+ CodeCell.prototype.append_output = function (json) {
+ this.expand();
+ if (json.output_type === 'pyout') {
+ this.append_pyout(json);
+ } else if (json.output_type === 'pyerr') {
+ this.append_pyerr(json);
+ } else if (json.output_type === 'display_data') {
+ this.append_display_data(json);
+ } else if (json.output_type === 'stream') {
+ this.append_stream(json);
+ };
+ this.outputs.push(json);
+ };
+
+
+ CodeCell.prototype.append_pyout = function (json) {
+ n = json.prompt_number || ' ';
var toinsert = $("<div/>").addClass("output_area output_pyout hbox");
toinsert.append($('<div/>').
addClass('prompt output_prompt').
html('Out[' + n + ']:')
);
- this.append_display_data(data, toinsert);
+ this.append_mime_type(json, toinsert);
toinsert.children().last().addClass("box_flex1");
this.element.find("div.output").append(toinsert);
// If we just output latex, typeset it.
- if (data["text/latex"] !== undefined) {
+ if (json.latex !== undefined) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
};
};
- CodeCell.prototype.append_pyerr = function (ename, evalue, tb) {
+ CodeCell.prototype.append_pyerr = function (json) {
+ var tb = json.traceback;
var s = '';
var len = tb.length;
for (var i=0; i<len; i++) {
s = s + tb[i] + '\n';
}
s = s + '\n';
- this.append_stream(s);
+ this.append_text(s);
+ };
+
+
+ CodeCell.prototype.append_stream = function (json) {
+ this.append_text(json.text);
};
- CodeCell.prototype.append_display_data = function (data, element) {
- if (data["text/html"] !== undefined) {
- this.append_html(data["text/html"], element);
- } else if (data["text/latex"] !== undefined) {
- this.append_latex(data["text/latex"], element);
+ CodeCell.prototype.append_display_data = function (json) {
+ this.append_mime_type(json);
+ };
+
+
+ CodeCell.prototype.append_mime_type = function (json, element) {
+ if (json.html !== undefined) {
+ this.append_html(json.html, element);
+ } else if (json.latex !== undefined) {
+ this.append_latex(json.latex, element);
// If it is undefined, then we just appended to div.output, which
// makes the latex visible and we can typeset it. The typesetting
// has to be done after the latex is on the page.
if (element === undefined) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
};
- } else if (data["image/svg+xml"] !== undefined) {
- this.append_svg(data["image/svg+xml"], element);
- } else if (data["image/png"] !== undefined) {
- this.append_png(data["image/png"], element);
- } else if (data["text/plain"] !== undefined) {
- this.append_stream(data["text/plain"], element);
+ } else if (json.svg !== undefined) {
+ this.append_svg(json.svg, element);
+ } else if (json.png !== undefined) {
+ this.append_png(json.png, element);
+ } else if (json.text !== undefined) {
+ this.append_text(json.text, element);
};
return element;
};
@@ -205,7 +233,7 @@ var IPython = (function (IPython) {
}
- CodeCell.prototype.append_stream = function (data, element) {
+ CodeCell.prototype.append_text = function (data, element) {
element = element || this.element.find("div.output");
var toinsert = $("<div/>").addClass("output_area output_stream");
toinsert.append($("<pre/>").html(utils.fixConsole(data)));
@@ -245,6 +273,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.clear_output = function () {
this.element.find("div.output").html("");
+ this.outputs = [];
};
@@ -301,6 +330,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.fromJSON = function (data) {
+ // console.log('Import from JSON:', data);
if (data.cell_type === 'code') {
if (data.input !== undefined) {
this.set_code(data.input);
@@ -310,22 +340,33 @@ var IPython = (function (IPython) {
} else {
this.set_input_prompt();
};
+ var len = data.outputs.length;
+ for (var i=0; i<len; i++) {
+ this.append_output(data.outputs[i]);
+ };
};
};
CodeCell.prototype.toJSON = function () {
- var data = {}
+ var data = {};
data.input = this.get_code();
data.cell_type = 'code';
if (this.input_prompt_number !== ' ') {
data.prompt_number = this.input_prompt_number
};
- data.outputs = [];
+ var outputs = [];
+ var len = this.outputs.length;
+ for (var i=0; i<len; i++) {
+ outputs[i] = this.outputs[i];
+ };
+ data.outputs = outputs;
data.language = 'python';
+ // console.log('Export to JSON:',data);
return data;
};
+
IPython.CodeCell = CodeCell;
return IPython;
View
59 IPython/frontend/html/notebook/static/js/notebook.js
@@ -431,18 +431,9 @@ var IPython = (function (IPython) {
// console.log(reply);
var msg_type = reply.header.msg_type;
var cell = this.cell_for_msg(reply.parent_header.msg_id);
- if (msg_type === "stream") {
- cell.expand();
- cell.append_stream(content.data + "\n");
- } else if (msg_type === "display_data") {
- cell.expand();
- cell.append_display_data(content.data);
- } else if (msg_type === "pyout") {
- cell.expand();
- cell.append_pyout(content.data, content.execution_count)
- } else if (msg_type === "pyerr") {
- cell.expand();
- cell.append_pyerr(content.ename, content.evalue, content.traceback);
+ var output_types = ['stream','display_data','pyout','pyerr'];
+ if (output_types.indexOf(msg_type) >= 0) {
+ this.handle_output(cell, msg_type, content);
} else if (msg_type === "status") {
if (content.execution_state === "busy") {
IPython.kernel_status_widget.status_busy();
@@ -453,6 +444,50 @@ var IPython = (function (IPython) {
};
+ Notebook.prototype.handle_output = function (cell, msg_type, content) {
+ var json = {};
+ json.output_type = msg_type;
+ if (msg_type === "stream") {
+ json.text = content.data + '\n';
+ } else if (msg_type === "display_data") {
+ json = this.convert_mime_types(json, content.data);
+ } else if (msg_type === "pyout") {
+ json.prompt_number = content.execution_count;
+ json = this.convert_mime_types(json, content.data);
+ } else if (msg_type === "pyerr") {
+ json.ename = content.ename;
+ json.evalue = content.evalue;
+ json.traceback = content.traceback;
+ };
+ cell.append_output(json);
+ };
+
+
+ Notebook.prototype.convert_mime_types = function (json, data) {
+ if (data['text/plain'] !== undefined) {
+ json.text = data['text/plain'];
+ };
+ if (data['text/html'] !== undefined) {
+ json.html = data['text/html'];
+ };
+ if (data['image/svg+xml'] !== undefined) {
+ json.svg = data['image/svg+xml'];
+ };
+ if (data['image/png'] !== undefined) {
+ json.png = data['image/png'];
+ };
+ if (data['text/latex'] !== undefined) {
+ json.latex = data['text/latex'];
+ };
+ if (data['application/json'] !== undefined) {
+ json.json = data['application/json'];
+ };
+ if (data['application/javascript'] !== undefined) {
+ json.javascript = data['application/javascript'];
+ }
+ return json;
+ };
+
Notebook.prototype.kernel_started = function () {
console.log("Kernel started: ", this.kernel.kernel_id);
this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
View
5 IPython/nbformat/v2/nbbase.py
@@ -24,7 +24,7 @@ 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_javascript=None, prompt_number=None):
"""Create a new code cell with input and output"""
output = NotebookNode()
if output_type is not None:
@@ -43,7 +43,8 @@ def new_output(output_type=None, output_text=None, output_png=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)
return output
View
5 IPython/nbformat/v2/nbxml.py
@@ -87,6 +87,7 @@ def to_notebook(self, root, **kwargs):
language = _get_text(cell_e,'language')
outputs = []
for output_e in cell_e.find('outputs').getiterator('output'):
+ 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')
@@ -98,7 +99,8 @@ def to_notebook(self, root, **kwargs):
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_json=output_json,output_javascript=output_javascript,
+ prompt_number=prompt_number
)
outputs.append(output)
cc = new_code_cell(input=input,prompt_number=prompt_number,
@@ -136,6 +138,7 @@ def writes(self, nb, **kwargs):
outputs_e = ET.SubElement(cell_e, 'outputs')
for output in cell.outputs:
output_e = ET.SubElement(outputs_e, 'output')
+ _set_int(cell,'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')
Please sign in to comment.
Something went wrong with that request. Please try again.