diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index de3030e..6f42eec 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/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 = $("
").addClass("output_area output_pyout hbox"); toinsert.append($('
'). 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").addClass("output_area output_stream"); toinsert.append($("
").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= 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);
diff --git a/IPython/nbformat/v2/nbbase.py b/IPython/nbformat/v2/nbbase.py
index df61687..b2256cb 100644
--- a/IPython/nbformat/v2/nbbase.py
+++ b/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
 
 
diff --git a/IPython/nbformat/v2/nbxml.py b/IPython/nbformat/v2/nbxml.py
index 2d7d7b4..2eabb3d 100644
--- a/IPython/nbformat/v2/nbxml.py
+++ b/IPython/nbformat/v2/nbxml.py
@@ -87,6 +87,7 @@ class XMLReader(NotebookReader):
                     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 @@ class XMLReader(NotebookReader):
                         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 @@ class XMLWriter(NotebookWriter):
                     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')