##// END OF EJS Templates
fix stream saving
Paul Ivanov -
Show More
@@ -1,779 +1,779
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // OutputArea
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule OutputArea
16 16 */
17 17 var IPython = (function (IPython) {
18 18 "use strict";
19 19
20 20 var utils = IPython.utils;
21 21
22 22 /**
23 23 * @class OutputArea
24 24 *
25 25 * @constructor
26 26 */
27 27
28 28 var OutputArea = function (selector, prompt_area) {
29 29 this.selector = selector;
30 30 this.wrapper = $(selector);
31 31 this.outputs = [];
32 32 this.collapsed = false;
33 33 this.scrolled = false;
34 34 this.clear_queued = null;
35 35 if (prompt_area === undefined) {
36 36 this.prompt_area = true;
37 37 } else {
38 38 this.prompt_area = prompt_area;
39 39 }
40 40 this.create_elements();
41 41 this.style();
42 42 this.bind_events();
43 43 };
44 44
45 45 OutputArea.prototype.create_elements = function () {
46 46 this.element = $("<div/>");
47 47 this.collapse_button = $("<div/>");
48 48 this.prompt_overlay = $("<div/>");
49 49 this.wrapper.append(this.prompt_overlay);
50 50 this.wrapper.append(this.element);
51 51 this.wrapper.append(this.collapse_button);
52 52 };
53 53
54 54
55 55 OutputArea.prototype.style = function () {
56 56 this.collapse_button.hide();
57 57 this.prompt_overlay.hide();
58 58
59 59 this.wrapper.addClass('output_wrapper');
60 60 this.element.addClass('output');
61 61
62 62 this.collapse_button.addClass("btn output_collapsed");
63 63 this.collapse_button.attr('title', 'click to expand output');
64 64 this.collapse_button.html('. . .');
65 65
66 66 this.prompt_overlay.addClass('out_prompt_overlay prompt');
67 67 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
68 68
69 69 this.collapse();
70 70 };
71 71
72 72 /**
73 73 * Should the OutputArea scroll?
74 74 * Returns whether the height (in lines) exceeds a threshold.
75 75 *
76 76 * @private
77 77 * @method _should_scroll
78 78 * @param [lines=100]{Integer}
79 79 * @return {Bool}
80 80 *
81 81 */
82 82 OutputArea.prototype._should_scroll = function (lines) {
83 83 if (lines <=0 ){ return }
84 84 if (!lines) {
85 85 lines = 100;
86 86 }
87 87 // line-height from http://stackoverflow.com/questions/1185151
88 88 var fontSize = this.element.css('font-size');
89 89 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
90 90
91 91 return (this.element.height() > lines * lineHeight);
92 92 };
93 93
94 94
95 95 OutputArea.prototype.bind_events = function () {
96 96 var that = this;
97 97 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
98 98 this.prompt_overlay.click(function () { that.toggle_scroll(); });
99 99
100 100 this.element.resize(function () {
101 101 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
102 102 if ( IPython.utils.browser[0] === "Firefox" ) {
103 103 return;
104 104 }
105 105 // maybe scroll output,
106 106 // if it's grown large enough and hasn't already been scrolled.
107 107 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
108 108 that.scroll_area();
109 109 }
110 110 });
111 111 this.collapse_button.click(function () {
112 112 that.expand();
113 113 });
114 114 };
115 115
116 116
117 117 OutputArea.prototype.collapse = function () {
118 118 if (!this.collapsed) {
119 119 this.element.hide();
120 120 this.prompt_overlay.hide();
121 121 if (this.element.html()){
122 122 this.collapse_button.show();
123 123 }
124 124 this.collapsed = true;
125 125 }
126 126 };
127 127
128 128
129 129 OutputArea.prototype.expand = function () {
130 130 if (this.collapsed) {
131 131 this.collapse_button.hide();
132 132 this.element.show();
133 133 this.prompt_overlay.show();
134 134 this.collapsed = false;
135 135 }
136 136 };
137 137
138 138
139 139 OutputArea.prototype.toggle_output = function () {
140 140 if (this.collapsed) {
141 141 this.expand();
142 142 } else {
143 143 this.collapse();
144 144 }
145 145 };
146 146
147 147
148 148 OutputArea.prototype.scroll_area = function () {
149 149 this.element.addClass('output_scroll');
150 150 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
151 151 this.scrolled = true;
152 152 };
153 153
154 154
155 155 OutputArea.prototype.unscroll_area = function () {
156 156 this.element.removeClass('output_scroll');
157 157 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
158 158 this.scrolled = false;
159 159 };
160 160
161 161 /**
162 162 * Threshold to trigger autoscroll when the OutputArea is resized,
163 163 * typically when new outputs are added.
164 164 *
165 165 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
166 166 * unless it is < 0, in which case autoscroll will never be triggered
167 167 *
168 168 * @property auto_scroll_threshold
169 169 * @type Number
170 170 * @default 100
171 171 *
172 172 **/
173 173 OutputArea.auto_scroll_threshold = 100;
174 174
175 175
176 176 /**
177 177 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
178 178 * shorter than this are never scrolled.
179 179 *
180 180 * @property minimum_scroll_threshold
181 181 * @type Number
182 182 * @default 20
183 183 *
184 184 **/
185 185 OutputArea.minimum_scroll_threshold = 20;
186 186
187 187
188 188 /**
189 189 *
190 190 * Scroll OutputArea if height supperior than a threshold (in lines).
191 191 *
192 192 * Threshold is a maximum number of lines. If unspecified, defaults to
193 193 * OutputArea.minimum_scroll_threshold.
194 194 *
195 195 * Negative threshold will prevent the OutputArea from ever scrolling.
196 196 *
197 197 * @method scroll_if_long
198 198 *
199 199 * @param [lines=20]{Number} Default to 20 if not set,
200 200 * behavior undefined for value of `0`.
201 201 *
202 202 **/
203 203 OutputArea.prototype.scroll_if_long = function (lines) {
204 204 var n = lines | OutputArea.minimum_scroll_threshold;
205 205 if(n <= 0){
206 206 return
207 207 }
208 208
209 209 if (this._should_scroll(n)) {
210 210 // only allow scrolling long-enough output
211 211 this.scroll_area();
212 212 }
213 213 };
214 214
215 215
216 216 OutputArea.prototype.toggle_scroll = function () {
217 217 if (this.scrolled) {
218 218 this.unscroll_area();
219 219 } else {
220 220 // only allow scrolling long-enough output
221 221 this.scroll_if_long();
222 222 }
223 223 };
224 224
225 225
226 226 // typeset with MathJax if MathJax is available
227 227 OutputArea.prototype.typeset = function () {
228 228 if (window.MathJax){
229 229 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
230 230 }
231 231 };
232 232
233 233
234 234 OutputArea.prototype.handle_output = function (msg) {
235 235 var json = {};
236 236 var msg_type = json.output_type = msg.header.msg_type;
237 237 var content = msg.content;
238 238 if (msg_type === "stream") {
239 239 json.text = content.data;
240 240 json.stream = content.name;
241 241 } else if (msg_type === "display_data") {
242 242 json = content.data
243 243 json.output_type = msg_type
244 244 json.metadata = content.metadata
245 245 } else if (msg_type === "pyout") {
246 246 json = content.data
247 247 json.output_type = msg_type
248 248 json.metadata = content.metadata
249 249 json.prompt_number = content.execution_count;
250 250 } else if (msg_type === "pyerr") {
251 251 json.ename = content.ename;
252 252 json.evalue = content.evalue;
253 253 json.traceback = content.traceback;
254 254 }
255 255 // append with dynamic=true
256 256 this.append_output(json, true);
257 257 };
258 258
259 259 OutputArea.mime_map = {
260 260 "text/plain" : "text",
261 261 "text/html" : "html",
262 262 "image/svg+xml" : "svg",
263 263 "image/png" : "png",
264 264 "image/jpeg" : "jpeg",
265 265 "text/latex" : "latex",
266 266 "application/json" : "json",
267 267 "application/javascript" : "javascript",
268 268 };
269 269
270 270 OutputArea.mime_map_r = {
271 271 "text" : "text/plain",
272 272 "html" : "text/html",
273 273 "svg" : "image/svg+xml",
274 274 "png" : "image/png",
275 275 "jpeg" : "image/jpeg",
276 276 "latex" : "text/latex",
277 277 "json" : "application/json",
278 278 "javascript" : "application/javascript",
279 279 };
280 280
281 281 OutputArea.prototype.rename_keys = function (data, key_map) {
282 282 var remapped = {};
283 283 for (var key in data) {
284 284 var new_key = key_map[key] || key;
285 285 remapped[new_key] = data[key];
286 286 }
287 287 return remapped;
288 288 };
289 289
290 290
291 291 OutputArea.prototype.append_output = function (json, dynamic) {
292 292 // If dynamic is true, javascript output will be eval'd.
293 293 this.expand();
294 294 // Clear the output if clear is queued.
295 295 var needs_height_reset = false;
296 296 if (this.clear_queued) {
297 297 this.clear_output(false);
298 298 needs_height_reset = true;
299 299 }
300 300
301 301 if (json.output_type === 'pyout') {
302 302 this.append_pyout(json, dynamic);
303 303 } else if (json.output_type === 'pyerr') {
304 304 this.append_pyerr(json);
305 305 } else if (json.output_type === 'display_data') {
306 306 this.append_display_data(json, dynamic);
307 307 } else if (json.output_type === 'stream') {
308 308 this.append_stream(json);
309 309 }
310 310 this.outputs.push(json);
311 311
312 312 // Only reset the height to automatic if the height is currently
313 313 // fixed (done by wait=True flag on clear_output).
314 314 if (needs_height_reset) {
315 315 this.element.height('');
316 316 }
317 317
318 318 var that = this;
319 319 setTimeout(function(){that.element.trigger('resize');}, 100);
320 320 };
321 321
322 322
323 323 OutputArea.prototype.create_output_area = function () {
324 324 var oa = $("<div/>").addClass("output_area");
325 325 if (this.prompt_area) {
326 326 oa.append($('<div/>').addClass('prompt'));
327 327 }
328 328 return oa;
329 329 };
330 330
331 331
332 332 function _get_metadata_key(metadata, key, mime) {
333 333 var mime_md = metadata[mime];
334 334 // mime-specific higher priority
335 335 if (mime_md && mime_md[key] !== undefined) {
336 console.log("got" + key + " "+ mime_md[key]);
337 336 return mime_md[key];
338 337 }
339 338 // fallback on global
340 339 return metadata[key];
341 340 }
342 341
343 342 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
344 343 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
345 344 if (_get_metadata_key(md, 'isolated', mime)) {
346 345 // Create an iframe to isolate the subarea from the rest of the
347 346 // document
348 347 var iframe = $('<iframe/>').addClass('box-flex1');
349 348 iframe.css({'height':1, 'width':'100%', 'display':'block'});
350 349 iframe.attr('frameborder', 0);
351 350 iframe.attr('scrolling', 'auto');
352 351
353 352 // Once the iframe is loaded, the subarea is dynamically inserted
354 353 iframe.on('load', function() {
355 354 // Workaround needed by Firefox, to properly render svg inside
356 355 // iframes, see http://stackoverflow.com/questions/10177190/
357 356 // svg-dynamically-added-to-iframe-does-not-render-correctly
358 357 this.contentDocument.open();
359 358
360 359 // Insert the subarea into the iframe
361 360 // We must directly write the html. When using Jquery's append
362 361 // method, javascript is evaluated in the parent document and
363 362 // not in the iframe document.
364 363 this.contentDocument.write(subarea.html());
365 364
366 365 this.contentDocument.close();
367 366
368 367 var body = this.contentDocument.body;
369 368 // Adjust the iframe height automatically
370 369 iframe.height(body.scrollHeight + 'px');
371 370 });
372 371
373 372 // Elements should be appended to the inner subarea and not to the
374 373 // iframe
375 374 iframe.append = function(that) {
376 375 subarea.append(that);
377 376 };
378 377
379 378 return iframe;
380 379 } else {
381 380 return subarea;
382 381 }
383 382 }
384 383
385 384
386 385 OutputArea.prototype._append_javascript_error = function (err, element) {
387 386 // display a message when a javascript error occurs in display output
388 387 var msg = "Javascript error adding output!"
389 388 if ( element === undefined ) return;
390 389 element.append(
391 390 $('<div/>').html(msg + "<br/>" +
392 391 err.toString() +
393 392 '<br/>See your browser Javascript console for more details.'
394 393 ).addClass('js-error')
395 394 );
396 395 };
397 396
398 397 OutputArea.prototype._safe_append = function (toinsert) {
399 398 // safely append an item to the document
400 399 // this is an object created by user code,
401 400 // and may have errors, which should not be raised
402 401 // under any circumstances.
403 402 try {
404 403 this.element.append(toinsert);
405 404 } catch(err) {
406 405 console.log(err);
407 406 // Create an actual output_area and output_subarea, which creates
408 407 // the prompt area and the proper indentation.
409 408 var toinsert = this.create_output_area();
410 409 var subarea = $('<div/>').addClass('output_subarea');
411 410 toinsert.append(subarea);
412 411 this._append_javascript_error(err, subarea);
413 412 this.element.append(toinsert);
414 413 }
415 414 };
416 415
417 416
418 417 OutputArea.prototype.append_pyout = function (json, dynamic) {
419 418 var n = json.prompt_number || ' ';
420 419 var toinsert = this.create_output_area();
421 420 if (this.prompt_area) {
422 421 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
423 422 }
424 423 this.append_mime_type(json, toinsert, dynamic);
425 424 this._safe_append(toinsert);
426 425 // If we just output latex, typeset it.
427 426 if ((json.latex !== undefined) || (json.html !== undefined)) {
428 427 this.typeset();
429 428 }
430 429 };
431 430
432 431
433 432 OutputArea.prototype.append_pyerr = function (json) {
434 433 var tb = json.traceback;
435 434 if (tb !== undefined && tb.length > 0) {
436 435 var s = '';
437 436 var len = tb.length;
438 437 for (var i=0; i<len; i++) {
439 438 s = s + tb[i] + '\n';
440 439 }
441 440 s = s + '\n';
442 441 var toinsert = this.create_output_area();
443 442 this.append_text(s, {}, toinsert);
444 443 this._safe_append(toinsert);
445 444 }
446 445 };
447 446
448 447
449 448 OutputArea.prototype.append_stream = function (json) {
450 449 // temporary fix: if stream undefined (json file written prior to this patch),
451 450 // default to most likely stdout:
452 451 if (json.stream == undefined){
453 452 json.stream = 'stdout';
454 453 }
455 454 var text = json.text;
456 455 var subclass = "output_"+json.stream;
457 456 if (this.outputs.length > 0){
458 457 // have at least one output to consider
459 458 var last = this.outputs[this.outputs.length-1];
460 459 if (last.output_type == 'stream' && json.stream == last.stream){
461 460 // latest output was in the same stream,
462 461 // so append directly into its pre tag
463 462 // escape ANSI & HTML specials:
464 463 var pre = this.element.find('div.'+subclass).last().find('pre');
465 464 var html = utils.fixCarriageReturn(
466 465 pre.html() + utils.fixConsole(text));
467 466 pre.html(html);
468 467 return;
469 468 }
470 469 }
471 470
472 471 if (!text.replace("\r", "")) {
473 472 // text is nothing (empty string, \r, etc.)
474 473 // so don't append any elements, which might add undesirable space
475 474 return;
476 475 }
477 476
478 477 // If we got here, attach a new div
479 478 var toinsert = this.create_output_area();
480 479 this.append_text(text, {}, toinsert, "output_stream "+subclass);
481 480 this._safe_append(toinsert);
482 481 };
483 482
484 483
485 484 OutputArea.prototype.append_display_data = function (json, dynamic) {
486 485 var toinsert = this.create_output_area();
487 486 if (this.append_mime_type(json, toinsert, dynamic)) {
488 487 this._safe_append(toinsert);
489 488 // If we just output latex, typeset it.
490 489 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
491 490 this.typeset();
492 491 }
493 492 }
494 493 };
495 494
496 495 OutputArea.display_order = [
497 496 'application/javascript',
498 497 'text/html',
499 498 'text/latex',
500 499 'image/svg+xml',
501 500 'image/png',
502 501 'image/jpeg',
503 502 'text/plain'
504 503 ];
505 504
506 505 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
507 506
508 507 for(var type_i in OutputArea.display_order){
509 508 var type = OutputArea.display_order[type_i];
510 509 if(json[type] != undefined ){
511 510 var md = {};
512 511 if (json.metadata) {
513 512 md = json.metadata;
514 513 }
515 514 if(type == 'javascript'){
516 515 if (dynamic) {
517 516 this.append_javascript(json.javascript, md, element, dynamic);
518 517 return true;
519 518 }
520 519 } else {
521 520 var old_name = OutputArea.mime_map[type]
522 521 this['append_'+old_name](json[type], md, element, type);
523 522 return true;
524 523 }
525 524 return false;
526 525 }
527 526 }
528 527 return false;
529 528 };
530 529
531 530
532 531 OutputArea.prototype.append_html = function (html, md, element, type) {
533 532 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
534 533 IPython.keyboard_manager.register_events(toinsert);
535 534 toinsert.append(html);
536 535 element.append(toinsert);
537 536 };
538 537
539 538
540 539 OutputArea.prototype.append_javascript = function (js, md, container, type) {
541 540 // We just eval the JS code, element appears in the local scope.
542 541 var element = this.create_output_subarea(md, "output_javascript", type);
543 542 IPython.keyboard_manager.register_events(element);
544 543 container.append(element);
545 544 try {
546 545 eval(js);
547 546 } catch(err) {
548 547 console.log(err);
549 548 this._append_javascript_error(err, element);
550 549 }
551 550 };
552 551
553 552
554 553 OutputArea.prototype.append_text = function (data, md, element, extra_class, type) {
555 554 var toinsert = this.create_output_subarea(md, "output_text", type);
556 555 // escape ANSI & HTML specials in plaintext:
557 556 data = utils.fixConsole(data);
558 557 data = utils.fixCarriageReturn(data);
559 558 data = utils.autoLinkUrls(data);
560 559 if (extra_class){
561 560 toinsert.addClass(extra_class);
562 561 }
563 562 toinsert.append($("<pre/>").html(data));
564 563 element.append(toinsert);
565 564 };
566 565
567 566
568 567 OutputArea.prototype.append_svg = function (svg, md, element, type) {
569 568 var toinsert = this.create_output_subarea(md, "output_svg", type);
570 569 toinsert.append(svg);
571 570 element.append(toinsert);
572 571 };
573 572
574 573
575 574 OutputArea.prototype._dblclick_to_reset_size = function (img) {
576 575 // schedule wrapping image in resizable after a delay,
577 576 // so we don't end up calling resize on a zero-size object
578 577 var that = this;
579 578 setTimeout(function () {
580 579 var h0 = img.height();
581 580 var w0 = img.width();
582 581 if (!(h0 && w0)) {
583 582 // zero size, schedule another timeout
584 583 that._dblclick_to_reset_size(img);
585 584 return;
586 585 }
587 586 img.resizable({
588 587 aspectRatio: true,
589 588 autoHide: true
590 589 });
591 590 img.dblclick(function () {
592 591 // resize wrapper & image together for some reason:
593 592 img.parent().height(h0);
594 593 img.height(h0);
595 594 img.parent().width(w0);
596 595 img.width(w0);
597 596 });
598 597 }, 250);
599 598 };
600 599
601 600
602 601 OutputArea.prototype.append_png = function (png, md, element, type) {
603 602 var toinsert = this.create_output_subarea(md, "output_png", type);
604 603 var img = $("<img/>");
605 604 img[0].setAttribute('src','data:image/png;base64,'+png);
606 605 if (md['height']) {
607 606 img[0].setAttribute('height', md['height']);
608 607 }
609 608 if (md['width']) {
610 609 img[0].setAttribute('width', md['width']);
611 610 }
612 611 this._dblclick_to_reset_size(img);
613 612 toinsert.append(img);
614 613 element.append(toinsert);
615 614 };
616 615
617 616
618 617 OutputArea.prototype.append_jpeg = function (jpeg, md, element, type) {
619 618 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
620 619 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
621 620 if (md['height']) {
622 621 img.attr('height', md['height']);
623 622 }
624 623 if (md['width']) {
625 624 img.attr('width', md['width']);
626 625 }
627 626 this._dblclick_to_reset_size(img);
628 627 toinsert.append(img);
629 628 element.append(toinsert);
630 629 };
631 630
632 631
633 632 OutputArea.prototype.append_latex = function (latex, md, element, type) {
634 633 // This method cannot do the typesetting because the latex first has to
635 634 // be on the page.
636 635 var toinsert = this.create_output_subarea(md, "output_latex", type);
637 636 toinsert.append(latex);
638 637 element.append(toinsert);
639 638 };
640 639
641 640 OutputArea.prototype.append_raw_input = function (msg) {
642 641 var that = this;
643 642 this.expand();
644 643 var content = msg.content;
645 644 var area = this.create_output_area();
646 645
647 646 // disable any other raw_inputs, if they are left around
648 647 $("div.output_subarea.raw_input").remove();
649 648
650 649 area.append(
651 650 $("<div/>")
652 651 .addClass("box-flex1 output_subarea raw_input")
653 652 .append(
654 653 $("<span/>")
655 654 .addClass("input_prompt")
656 655 .text(content.prompt)
657 656 )
658 657 .append(
659 658 $("<input/>")
660 659 .addClass("raw_input")
661 660 .attr('type', 'text')
662 661 .attr("size", 47)
663 662 .keydown(function (event, ui) {
664 663 // make sure we submit on enter,
665 664 // and don't re-execute the *cell* on shift-enter
666 665 if (event.which === utils.keycodes.ENTER) {
667 666 that._submit_raw_input();
668 667 return false;
669 668 }
670 669 })
671 670 )
672 671 );
673 672
674 673 this.element.append(area);
675 674 var raw_input = area.find('input.raw_input');
676 675 // Register events that enable/disable the keyboard manager while raw
677 676 // input is focused.
678 677 IPython.keyboard_manager.register_events(raw_input);
679 678 // Note, the following line used to read raw_input.focus().focus().
680 679 // This seemed to be needed otherwise only the cell would be focused.
681 680 // But with the modal UI, this seems to work fine with one call to focus().
682 681 raw_input.focus();
683 682 }
684 683
685 684 OutputArea.prototype._submit_raw_input = function (evt) {
686 685 var container = this.element.find("div.raw_input");
687 686 var theprompt = container.find("span.input_prompt");
688 687 var theinput = container.find("input.raw_input");
689 688 var value = theinput.val();
690 689 var content = {
691 690 output_type : 'stream',
692 691 name : 'stdout',
693 692 text : theprompt.text() + value + '\n'
694 693 }
695 694 // remove form container
696 695 container.parent().remove();
697 696 // replace with plaintext version in stdout
698 697 this.append_output(content, false);
699 698 $([IPython.events]).trigger('send_input_reply.Kernel', value);
700 699 }
701 700
702 701
703 702 OutputArea.prototype.handle_clear_output = function (msg) {
704 703 this.clear_output(msg.content.wait);
705 704 };
706 705
707 706
708 707 OutputArea.prototype.clear_output = function(wait) {
709 708 if (wait) {
710 709
711 710 // If a clear is queued, clear before adding another to the queue.
712 711 if (this.clear_queued) {
713 712 this.clear_output(false);
714 713 };
715 714
716 715 this.clear_queued = true;
717 716 } else {
718 717
719 718 // Fix the output div's height if the clear_output is waiting for
720 719 // new output (it is being used in an animation).
721 720 if (this.clear_queued) {
722 721 var height = this.element.height();
723 722 this.element.height(height);
724 723 this.clear_queued = false;
725 724 }
726 725
727 726 // clear all, no need for logic
728 727 this.element.html("");
729 728 this.outputs = [];
730 729 this.unscroll_area();
731 730 return;
732 731 };
733 732 };
734 733
735 734
736 735 // JSON serialization
737 736
738 737 OutputArea.prototype.fromJSON = function (outputs) {
739 738 // add here the mapping of short key to mime type
740 739 // TODO: remove this when we update to nbformat 4
741 740 var len = outputs.length;
742 741 var data;
743 742 for (var i=0; i<len; i++) {
744 var msg_type = outputs[i].output_type;
743 data = outputs[i];
744 var msg_type = data.output_type;
745 745 if (msg_type === "display_data" || msg_type === "pyout") {
746 746 // convert short keys to mime keys
747 data = this.rename_keys(outputs[i], OutputArea.mime_map_r);
748 data.metadata = this.rename_keys(outputs[i].metadata, OutputArea.mime_map_r);
747 data = this.rename_keys(data, OutputArea.mime_map_r);
748 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r);
749 749 }
750 750
751 751 // append with dynamic=false.
752 752 this.append_output(data, false);
753 753 }
754 754 };
755 755
756 756
757 757 OutputArea.prototype.toJSON = function () {
758 758 var outputs = [];
759 759 var len = this.outputs.length;
760 760 var data;
761 761 for (var i=0; i<len; i++) {
762 var msg_type = this.outputs[i].output_type;
763 console.log("msg type is ", msg_type)
762 data = this.outputs[i];
763 var msg_type = data.output_type;
764 764 if (msg_type === "display_data" || msg_type === "pyout") {
765 765 // convert mime keys to short keys
766 data = this.rename_keys(this.outputs[i], OutputArea.mime_map);
767 data.metadata = this.rename_keys(this.outputs[i], OutputArea.mime_map);
766 data = this.rename_keys(data, OutputArea.mime_map);
767 data.metadata = this.rename_keys(data, OutputArea.mime_map);
768 768 }
769 769 outputs[i] = data;
770 770 }
771 771 return outputs;
772 772 };
773 773
774 774
775 775 IPython.OutputArea = OutputArea;
776 776
777 777 return IPython;
778 778
779 779 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now