##// 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 12 this.input_prompt_number = ' ';
13 13 this.is_completing = false;
14 14 this.completion_cursor = null;
15 this.outputs = [];
15 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 168 var toinsert = $("<div/>").addClass("output_area output_pyout hbox");
152 169 toinsert.append($('<div/>').
153 170 addClass('prompt output_prompt').
154 171 html('Out[' + n + ']:')
155 172 );
156 this.append_display_data(data, toinsert);
173 this.append_mime_type(json, toinsert);
157 174 toinsert.children().last().addClass("box_flex1");
158 175 this.element.find("div.output").append(toinsert);
159 176 // If we just output latex, typeset it.
160 if (data["text/latex"] !== undefined) {
177 if (json.latex !== undefined) {
161 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 185 var s = '';
168 186 var len = tb.length;
169 187 for (var i=0; i<len; i++) {
170 188 s = s + tb[i] + '\n';
171 189 }
172 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) {
178 if (data["text/html"] !== undefined) {
179 this.append_html(data["text/html"], element);
180 } else if (data["text/latex"] !== undefined) {
181 this.append_latex(data["text/latex"], element);
200 CodeCell.prototype.append_display_data = function (json) {
201 this.append_mime_type(json);
202 };
203
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 210 // If it is undefined, then we just appended to div.output, which
183 211 // makes the latex visible and we can typeset it. The typesetting
184 212 // has to be done after the latex is on the page.
185 213 if (element === undefined) {
186 214 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
187 215 };
188 } else if (data["image/svg+xml"] !== undefined) {
189 this.append_svg(data["image/svg+xml"], element);
190 } else if (data["image/png"] !== undefined) {
191 this.append_png(data["image/png"], element);
192 } else if (data["text/plain"] !== undefined) {
193 this.append_stream(data["text/plain"], element);
216 } else if (json.svg !== undefined) {
217 this.append_svg(json.svg, element);
218 } else if (json.png !== undefined) {
219 this.append_png(json.png, element);
220 } else if (json.text !== undefined) {
221 this.append_text(json.text, element);
194 222 };
195 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 237 element = element || this.element.find("div.output");
210 238 var toinsert = $("<div/>").addClass("output_area output_stream");
211 239 toinsert.append($("<pre/>").html(utils.fixConsole(data)));
@@ -245,6 +273,7 b' var IPython = (function (IPython) {'
245 273
246 274 CodeCell.prototype.clear_output = function () {
247 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 332 CodeCell.prototype.fromJSON = function (data) {
333 // console.log('Import from JSON:', data);
304 334 if (data.cell_type === 'code') {
305 335 if (data.input !== undefined) {
306 336 this.set_code(data.input);
@@ -310,22 +340,33 b' var IPython = (function (IPython) {'
310 340 } else {
311 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 351 CodeCell.prototype.toJSON = function () {
318 var data = {}
352 var data = {};
319 353 data.input = this.get_code();
320 354 data.cell_type = 'code';
321 355 if (this.input_prompt_number !== ' ') {
322 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 364 data.language = 'python';
365 // console.log('Export to JSON:',data);
326 366 return data;
327 367 };
328 368
369
329 370 IPython.CodeCell = CodeCell;
330 371
331 372 return IPython;
@@ -431,18 +431,9 b' var IPython = (function (IPython) {'
431 431 // console.log(reply);
432 432 var msg_type = reply.header.msg_type;
433 433 var cell = this.cell_for_msg(reply.parent_header.msg_id);
434 if (msg_type === "stream") {
435 cell.expand();
436 cell.append_stream(content.data + "\n");
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);
434 var output_types = ['stream','display_data','pyout','pyerr'];
435 if (output_types.indexOf(msg_type) >= 0) {
436 this.handle_output(cell, msg_type, content);
446 437 } else if (msg_type === "status") {
447 438 if (content.execution_state === "busy") {
448 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 491 Notebook.prototype.kernel_started = function () {
457 492 console.log("Kernel started: ", this.kernel.kernel_id);
458 493 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
@@ -24,7 +24,7 b' def from_dict(d):'
24 24
25 25 def new_output(output_type=None, output_text=None, output_png=None,
26 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 28 """Create a new code cell with input and output"""
29 29 output = NotebookNode()
30 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 43 output.json = unicode(output_json)
44 44 if output_javascript is not None:
45 45 output.javascript = unicode(output_javascript)
46
46 if prompt_number is not None:
47 output.prompt_number = int(prompt_number)
47 48 return output
48 49
49 50
@@ -87,6 +87,7 b' class XMLReader(NotebookReader):'
87 87 language = _get_text(cell_e,'language')
88 88 outputs = []
89 89 for output_e in cell_e.find('outputs').getiterator('output'):
90 prompt_number = _get_int(output_e,'prompt_number')
90 91 output_type = _get_text(output_e,'output_type')
91 92 output_text = _get_text(output_e,'text')
92 93 output_png = _get_binary(output_e,'png')
@@ -98,7 +99,8 b' class XMLReader(NotebookReader):'
98 99 output = new_output(output_type=output_type,output_png=output_png,
99 100 output_text=output_text,output_svg=output_svg,
100 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 105 outputs.append(output)
104 106 cc = new_code_cell(input=input,prompt_number=prompt_number,
@@ -136,6 +138,7 b' class XMLWriter(NotebookWriter):'
136 138 outputs_e = ET.SubElement(cell_e, 'outputs')
137 139 for output in cell.outputs:
138 140 output_e = ET.SubElement(outputs_e, 'output')
141 _set_int(cell,'prompt_number',output_e,'prompt_number')
139 142 _set_text(output,'output_type',output_e,'output_type')
140 143 _set_text(output,'text',output_e,'text')
141 144 _set_binary(output,'png',output_e,'png')
General Comments 0
You need to be logged in to leave comments. Login now