##// END OF EJS Templates
Added saving and loading of output of all types.
Brian E. Granger -
Show More
@@ -12,6 +12,7 b' var IPython = (function (IPython) {'
12 this.input_prompt_number = ' ';
12 this.input_prompt_number = ' ';
13 this.is_completing = false;
13 this.is_completing = false;
14 this.completion_cursor = null;
14 this.completion_cursor = null;
15 this.outputs = [];
15 IPython.Cell.apply(this, arguments);
16 IPython.Cell.apply(this, arguments);
16 };
17 };
17
18
@@ -147,50 +148,77 b' var IPython = (function (IPython) {'
147 };
148 };
148
149
149
150
150 CodeCell.prototype.append_pyout = function (data, n) {
151 CodeCell.prototype.append_output = function (json) {
152 this.expand();
153 if (json.output_type === 'pyout') {
154 this.append_pyout(json);
155 } else if (json.output_type === 'pyerr') {
156 this.append_pyerr(json);
157 } else if (json.output_type === 'display_data') {
158 this.append_display_data(json);
159 } else if (json.output_type === 'stream') {
160 this.append_stream(json);
161 };
162 this.outputs.push(json);
163 };
164
165
166 CodeCell.prototype.append_pyout = function (json) {
167 n = json.prompt_number || ' ';
151 var toinsert = $("<div/>").addClass("output_area output_pyout hbox");
168 var toinsert = $("<div/>").addClass("output_area output_pyout hbox");
152 toinsert.append($('<div/>').
169 toinsert.append($('<div/>').
153 addClass('prompt output_prompt').
170 addClass('prompt output_prompt').
154 html('Out[' + n + ']:')
171 html('Out[' + n + ']:')
155 );
172 );
156 this.append_display_data(data, toinsert);
173 this.append_mime_type(json, toinsert);
157 toinsert.children().last().addClass("box_flex1");
174 toinsert.children().last().addClass("box_flex1");
158 this.element.find("div.output").append(toinsert);
175 this.element.find("div.output").append(toinsert);
159 // If we just output latex, typeset it.
176 // If we just output latex, typeset it.
160 if (data["text/latex"] !== undefined) {
177 if (json.latex !== undefined) {
161 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
178 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
162 };
179 };
163 };
180 };
164
181
165
182
166 CodeCell.prototype.append_pyerr = function (ename, evalue, tb) {
183 CodeCell.prototype.append_pyerr = function (json) {
184 var tb = json.traceback;
167 var s = '';
185 var s = '';
168 var len = tb.length;
186 var len = tb.length;
169 for (var i=0; i<len; i++) {
187 for (var i=0; i<len; i++) {
170 s = s + tb[i] + '\n';
188 s = s + tb[i] + '\n';
171 }
189 }
172 s = s + '\n';
190 s = s + '\n';
173 this.append_stream(s);
191 this.append_text(s);
192 };
193
194
195 CodeCell.prototype.append_stream = function (json) {
196 this.append_text(json.text);
174 };
197 };
175
198
176
199
177 CodeCell.prototype.append_display_data = function (data, element) {
200 CodeCell.prototype.append_display_data = function (json) {
178 if (data["text/html"] !== undefined) {
201 this.append_mime_type(json);
179 this.append_html(data["text/html"], element);
202 };
180 } else if (data["text/latex"] !== undefined) {
203
181 this.append_latex(data["text/latex"], element);
204
205 CodeCell.prototype.append_mime_type = function (json, element) {
206 if (json.html !== undefined) {
207 this.append_html(json.html, element);
208 } else if (json.latex !== undefined) {
209 this.append_latex(json.latex, element);
182 // If it is undefined, then we just appended to div.output, which
210 // If it is undefined, then we just appended to div.output, which
183 // makes the latex visible and we can typeset it. The typesetting
211 // makes the latex visible and we can typeset it. The typesetting
184 // has to be done after the latex is on the page.
212 // has to be done after the latex is on the page.
185 if (element === undefined) {
213 if (element === undefined) {
186 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
214 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
187 };
215 };
188 } else if (data["image/svg+xml"] !== undefined) {
216 } else if (json.svg !== undefined) {
189 this.append_svg(data["image/svg+xml"], element);
217 this.append_svg(json.svg, element);
190 } else if (data["image/png"] !== undefined) {
218 } else if (json.png !== undefined) {
191 this.append_png(data["image/png"], element);
219 this.append_png(json.png, element);
192 } else if (data["text/plain"] !== undefined) {
220 } else if (json.text !== undefined) {
193 this.append_stream(data["text/plain"], element);
221 this.append_text(json.text, element);
194 };
222 };
195 return element;
223 return element;
196 };
224 };
@@ -205,7 +233,7 b' var IPython = (function (IPython) {'
205 }
233 }
206
234
207
235
208 CodeCell.prototype.append_stream = function (data, element) {
236 CodeCell.prototype.append_text = function (data, element) {
209 element = element || this.element.find("div.output");
237 element = element || this.element.find("div.output");
210 var toinsert = $("<div/>").addClass("output_area output_stream");
238 var toinsert = $("<div/>").addClass("output_area output_stream");
211 toinsert.append($("<pre/>").html(utils.fixConsole(data)));
239 toinsert.append($("<pre/>").html(utils.fixConsole(data)));
@@ -245,6 +273,7 b' var IPython = (function (IPython) {'
245
273
246 CodeCell.prototype.clear_output = function () {
274 CodeCell.prototype.clear_output = function () {
247 this.element.find("div.output").html("");
275 this.element.find("div.output").html("");
276 this.outputs = [];
248 };
277 };
249
278
250
279
@@ -301,6 +330,7 b' var IPython = (function (IPython) {'
301
330
302
331
303 CodeCell.prototype.fromJSON = function (data) {
332 CodeCell.prototype.fromJSON = function (data) {
333 // console.log('Import from JSON:', data);
304 if (data.cell_type === 'code') {
334 if (data.cell_type === 'code') {
305 if (data.input !== undefined) {
335 if (data.input !== undefined) {
306 this.set_code(data.input);
336 this.set_code(data.input);
@@ -310,22 +340,33 b' var IPython = (function (IPython) {'
310 } else {
340 } else {
311 this.set_input_prompt();
341 this.set_input_prompt();
312 };
342 };
343 var len = data.outputs.length;
344 for (var i=0; i<len; i++) {
345 this.append_output(data.outputs[i]);
346 };
313 };
347 };
314 };
348 };
315
349
316
350
317 CodeCell.prototype.toJSON = function () {
351 CodeCell.prototype.toJSON = function () {
318 var data = {}
352 var data = {};
319 data.input = this.get_code();
353 data.input = this.get_code();
320 data.cell_type = 'code';
354 data.cell_type = 'code';
321 if (this.input_prompt_number !== ' ') {
355 if (this.input_prompt_number !== ' ') {
322 data.prompt_number = this.input_prompt_number
356 data.prompt_number = this.input_prompt_number
323 };
357 };
324 data.outputs = [];
358 var outputs = [];
359 var len = this.outputs.length;
360 for (var i=0; i<len; i++) {
361 outputs[i] = this.outputs[i];
362 };
363 data.outputs = outputs;
325 data.language = 'python';
364 data.language = 'python';
365 // console.log('Export to JSON:',data);
326 return data;
366 return data;
327 };
367 };
328
368
369
329 IPython.CodeCell = CodeCell;
370 IPython.CodeCell = CodeCell;
330
371
331 return IPython;
372 return IPython;
@@ -431,18 +431,9 b' var IPython = (function (IPython) {'
431 // console.log(reply);
431 // console.log(reply);
432 var msg_type = reply.header.msg_type;
432 var msg_type = reply.header.msg_type;
433 var cell = this.cell_for_msg(reply.parent_header.msg_id);
433 var cell = this.cell_for_msg(reply.parent_header.msg_id);
434 if (msg_type === "stream") {
434 var output_types = ['stream','display_data','pyout','pyerr'];
435 cell.expand();
435 if (output_types.indexOf(msg_type) >= 0) {
436 cell.append_stream(content.data + "\n");
436 this.handle_output(cell, msg_type, content);
437 } else if (msg_type === "display_data") {
438 cell.expand();
439 cell.append_display_data(content.data);
440 } else if (msg_type === "pyout") {
441 cell.expand();
442 cell.append_pyout(content.data, content.execution_count)
443 } else if (msg_type === "pyerr") {
444 cell.expand();
445 cell.append_pyerr(content.ename, content.evalue, content.traceback);
446 } else if (msg_type === "status") {
437 } else if (msg_type === "status") {
447 if (content.execution_state === "busy") {
438 if (content.execution_state === "busy") {
448 IPython.kernel_status_widget.status_busy();
439 IPython.kernel_status_widget.status_busy();
@@ -453,6 +444,50 b' var IPython = (function (IPython) {'
453 };
444 };
454
445
455
446
447 Notebook.prototype.handle_output = function (cell, msg_type, content) {
448 var json = {};
449 json.output_type = msg_type;
450 if (msg_type === "stream") {
451 json.text = content.data + '\n';
452 } else if (msg_type === "display_data") {
453 json = this.convert_mime_types(json, content.data);
454 } else if (msg_type === "pyout") {
455 json.prompt_number = content.execution_count;
456 json = this.convert_mime_types(json, content.data);
457 } else if (msg_type === "pyerr") {
458 json.ename = content.ename;
459 json.evalue = content.evalue;
460 json.traceback = content.traceback;
461 };
462 cell.append_output(json);
463 };
464
465
466 Notebook.prototype.convert_mime_types = function (json, data) {
467 if (data['text/plain'] !== undefined) {
468 json.text = data['text/plain'];
469 };
470 if (data['text/html'] !== undefined) {
471 json.html = data['text/html'];
472 };
473 if (data['image/svg+xml'] !== undefined) {
474 json.svg = data['image/svg+xml'];
475 };
476 if (data['image/png'] !== undefined) {
477 json.png = data['image/png'];
478 };
479 if (data['text/latex'] !== undefined) {
480 json.latex = data['text/latex'];
481 };
482 if (data['application/json'] !== undefined) {
483 json.json = data['application/json'];
484 };
485 if (data['application/javascript'] !== undefined) {
486 json.javascript = data['application/javascript'];
487 }
488 return json;
489 };
490
456 Notebook.prototype.kernel_started = function () {
491 Notebook.prototype.kernel_started = function () {
457 console.log("Kernel started: ", this.kernel.kernel_id);
492 console.log("Kernel started: ", this.kernel.kernel_id);
458 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
493 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
@@ -24,7 +24,7 b' def from_dict(d):'
24
24
25 def new_output(output_type=None, output_text=None, output_png=None,
25 def new_output(output_type=None, output_text=None, output_png=None,
26 output_html=None, output_svg=None, output_latex=None, output_json=None,
26 output_html=None, output_svg=None, output_latex=None, output_json=None,
27 output_javascript=None):
27 output_javascript=None, prompt_number=None):
28 """Create a new code cell with input and output"""
28 """Create a new code cell with input and output"""
29 output = NotebookNode()
29 output = NotebookNode()
30 if output_type is not None:
30 if output_type is not None:
@@ -43,7 +43,8 b' def new_output(output_type=None, output_text=None, output_png=None,'
43 output.json = unicode(output_json)
43 output.json = unicode(output_json)
44 if output_javascript is not None:
44 if output_javascript is not None:
45 output.javascript = unicode(output_javascript)
45 output.javascript = unicode(output_javascript)
46
46 if prompt_number is not None:
47 output.prompt_number = int(prompt_number)
47 return output
48 return output
48
49
49
50
@@ -87,6 +87,7 b' class XMLReader(NotebookReader):'
87 language = _get_text(cell_e,'language')
87 language = _get_text(cell_e,'language')
88 outputs = []
88 outputs = []
89 for output_e in cell_e.find('outputs').getiterator('output'):
89 for output_e in cell_e.find('outputs').getiterator('output'):
90 prompt_number = _get_int(output_e,'prompt_number')
90 output_type = _get_text(output_e,'output_type')
91 output_type = _get_text(output_e,'output_type')
91 output_text = _get_text(output_e,'text')
92 output_text = _get_text(output_e,'text')
92 output_png = _get_binary(output_e,'png')
93 output_png = _get_binary(output_e,'png')
@@ -98,7 +99,8 b' class XMLReader(NotebookReader):'
98 output = new_output(output_type=output_type,output_png=output_png,
99 output = new_output(output_type=output_type,output_png=output_png,
99 output_text=output_text,output_svg=output_svg,
100 output_text=output_text,output_svg=output_svg,
100 output_html=output_html,output_latex=output_latex,
101 output_html=output_html,output_latex=output_latex,
101 output_json=output_json,output_javascript=output_javascript
102 output_json=output_json,output_javascript=output_javascript,
103 prompt_number=prompt_number
102 )
104 )
103 outputs.append(output)
105 outputs.append(output)
104 cc = new_code_cell(input=input,prompt_number=prompt_number,
106 cc = new_code_cell(input=input,prompt_number=prompt_number,
@@ -136,6 +138,7 b' class XMLWriter(NotebookWriter):'
136 outputs_e = ET.SubElement(cell_e, 'outputs')
138 outputs_e = ET.SubElement(cell_e, 'outputs')
137 for output in cell.outputs:
139 for output in cell.outputs:
138 output_e = ET.SubElement(outputs_e, 'output')
140 output_e = ET.SubElement(outputs_e, 'output')
141 _set_int(cell,'prompt_number',output_e,'prompt_number')
139 _set_text(output,'output_type',output_e,'output_type')
142 _set_text(output,'output_type',output_e,'output_type')
140 _set_text(output,'text',output_e,'text')
143 _set_text(output,'text',output_e,'text')
141 _set_binary(output,'png',output_e,'png')
144 _set_binary(output,'png',output_e,'png')
General Comments 0
You need to be logged in to leave comments. Login now