##// END OF EJS Templates
fix png inlining
Paul Ivanov -
Show More
@@ -1,785 +1,785
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 = this.convert_mime_types(json, content.data);
243 243 json.metadata = this.convert_mime_types({}, content.metadata);
244 244 } else if (msg_type === "pyout") {
245 245 json.prompt_number = content.execution_count;
246 246 json = this.convert_mime_types(json, content.data);
247 247 json.metadata = this.convert_mime_types({}, content.metadata);
248 248 } else if (msg_type === "pyerr") {
249 249 json.ename = content.ename;
250 250 json.evalue = content.evalue;
251 251 json.traceback = content.traceback;
252 252 }
253 253 // append with dynamic=true
254 254 this.append_output(json, true);
255 255 };
256 256
257 257 OutputArea.mime_map = {
258 258 "text/plain" : "text",
259 259 "text/html" : "html",
260 260 "image/svg+xml" : "svg",
261 261 "image/png" : "png",
262 262 "image/jpeg" : "jpeg",
263 263 "text/latex" : "latex",
264 264 "application/json" : "json",
265 265 "application/javascript" : "javascript",
266 266 };
267 267
268 268 OutputArea.mime_map_r = {
269 269 "text" : "text/plain",
270 270 "html" : "text/html",
271 271 "svg" : "image/svg+xml",
272 272 "png" : "image/png",
273 273 "jpeg" : "image/jpeg",
274 274 "latex" : "text/latex",
275 275 "json" : "application/json",
276 276 "javascript" : "application/javascript",
277 277 };
278 278
279 279 OutputArea.prototype.convert_mime_types = function (json, data) {
280 280 if (!data) {
281 281 return json;
282 282 }
283 283 // non-mimetype-keyed metadata used to get dropped here, this code
284 284 // re-injects it into the json.
285 285 for (var key in data) {
286 286 var rkey = OutputArea.mime_map_r[key] || key;
287 287 json[rkey] = data[key];
288 288 }
289 289 return json;
290 290 };
291 291
292 292 OutputArea.prototype.convert_mime_types_r = function (data) {
293 293 for (var key in data) {
294 294 var mkey = OutputArea.mime_map_r[key] || key;
295 295 if (mkey !== key) {
296 296 // move short keys to mime-type keys
297 297 data[mkey] = data[key];
298 298 delete data[key];
299 299 }
300 300 }
301 301 return data;
302 302 };
303 303
304 304
305 305 OutputArea.prototype.append_output = function (json, dynamic) {
306 306 // If dynamic is true, javascript output will be eval'd.
307 307 this.expand();
308 308 // Clear the output if clear is queued.
309 309 var needs_height_reset = false;
310 310 if (this.clear_queued) {
311 311 this.clear_output(false);
312 312 needs_height_reset = true;
313 313 }
314 314
315 315 if (json.output_type === 'pyout') {
316 316 this.append_pyout(json, dynamic);
317 317 } else if (json.output_type === 'pyerr') {
318 318 this.append_pyerr(json);
319 319 } else if (json.output_type === 'display_data') {
320 320 this.append_display_data(json, dynamic);
321 321 } else if (json.output_type === 'stream') {
322 322 this.append_stream(json);
323 323 }
324 324 this.outputs.push(json);
325 325
326 326 // Only reset the height to automatic if the height is currently
327 327 // fixed (done by wait=True flag on clear_output).
328 328 if (needs_height_reset) {
329 329 this.element.height('');
330 330 }
331 331
332 332 var that = this;
333 333 setTimeout(function(){that.element.trigger('resize');}, 100);
334 334 };
335 335
336 336
337 337 OutputArea.prototype.create_output_area = function () {
338 338 var oa = $("<div/>").addClass("output_area");
339 339 if (this.prompt_area) {
340 340 oa.append($('<div/>').addClass('prompt'));
341 341 }
342 342 return oa;
343 343 };
344 344
345 345
346 346 function _get_metadata_key(metadata, key, mime) {
347 347 var mime_md = metadata[mime];
348 348 // mime-specific higher priority
349 349 if (mime_md && mime_md[key] !== undefined) {
350 350 console.log("got" + key + " "+ mime_md[key]);
351 351 return mime_md[key];
352 352 }
353 353 // fallback on global
354 354 return metadata[key];
355 355 }
356 356
357 357 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
358 358 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
359 359 if (_get_metadata_key(md, 'isolated', mime)) {
360 360 // Create an iframe to isolate the subarea from the rest of the
361 361 // document
362 362 var iframe = $('<iframe/>').addClass('box-flex1');
363 363 iframe.css({'height':1, 'width':'100%', 'display':'block'});
364 364 iframe.attr('frameborder', 0);
365 365 iframe.attr('scrolling', 'auto');
366 366
367 367 // Once the iframe is loaded, the subarea is dynamically inserted
368 368 iframe.on('load', function() {
369 369 // Workaround needed by Firefox, to properly render svg inside
370 370 // iframes, see http://stackoverflow.com/questions/10177190/
371 371 // svg-dynamically-added-to-iframe-does-not-render-correctly
372 372 this.contentDocument.open();
373 373
374 374 // Insert the subarea into the iframe
375 375 // We must directly write the html. When using Jquery's append
376 376 // method, javascript is evaluated in the parent document and
377 377 // not in the iframe document.
378 378 this.contentDocument.write(subarea.html());
379 379
380 380 this.contentDocument.close();
381 381
382 382 var body = this.contentDocument.body;
383 383 // Adjust the iframe height automatically
384 384 iframe.height(body.scrollHeight + 'px');
385 385 });
386 386
387 387 // Elements should be appended to the inner subarea and not to the
388 388 // iframe
389 389 iframe.append = function(that) {
390 390 subarea.append(that);
391 391 };
392 392
393 393 return iframe;
394 394 } else {
395 395 return subarea;
396 396 }
397 397 }
398 398
399 399
400 400 OutputArea.prototype._append_javascript_error = function (err, element) {
401 401 // display a message when a javascript error occurs in display output
402 402 var msg = "Javascript error adding output!"
403 403 if ( element === undefined ) return;
404 404 element.append(
405 405 $('<div/>').html(msg + "<br/>" +
406 406 err.toString() +
407 407 '<br/>See your browser Javascript console for more details.'
408 408 ).addClass('js-error')
409 409 );
410 410 };
411 411
412 412 OutputArea.prototype._safe_append = function (toinsert) {
413 413 // safely append an item to the document
414 414 // this is an object created by user code,
415 415 // and may have errors, which should not be raised
416 416 // under any circumstances.
417 417 try {
418 418 this.element.append(toinsert);
419 419 } catch(err) {
420 420 console.log(err);
421 421 // Create an actual output_area and output_subarea, which creates
422 422 // the prompt area and the proper indentation.
423 423 var toinsert = this.create_output_area();
424 424 var subarea = $('<div/>').addClass('output_subarea');
425 425 toinsert.append(subarea);
426 426 this._append_javascript_error(err, subarea);
427 427 this.element.append(toinsert);
428 428 }
429 429 };
430 430
431 431
432 432 OutputArea.prototype.append_pyout = function (json, dynamic) {
433 433 var n = json.prompt_number || ' ';
434 434 var toinsert = this.create_output_area();
435 435 if (this.prompt_area) {
436 436 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
437 437 }
438 438 this.append_mime_type(json, toinsert, dynamic);
439 439 this._safe_append(toinsert);
440 440 // If we just output latex, typeset it.
441 441 if ((json.latex !== undefined) || (json.html !== undefined)) {
442 442 this.typeset();
443 443 }
444 444 };
445 445
446 446
447 447 OutputArea.prototype.append_pyerr = function (json) {
448 448 var tb = json.traceback;
449 449 if (tb !== undefined && tb.length > 0) {
450 450 var s = '';
451 451 var len = tb.length;
452 452 for (var i=0; i<len; i++) {
453 453 s = s + tb[i] + '\n';
454 454 }
455 455 s = s + '\n';
456 456 var toinsert = this.create_output_area();
457 457 this.append_text(s, {}, toinsert);
458 458 this._safe_append(toinsert);
459 459 }
460 460 };
461 461
462 462
463 463 OutputArea.prototype.append_stream = function (json) {
464 464 // temporary fix: if stream undefined (json file written prior to this patch),
465 465 // default to most likely stdout:
466 466 if (json.stream == undefined){
467 467 json.stream = 'stdout';
468 468 }
469 469 var text = json.text;
470 470 var subclass = "output_"+json.stream;
471 471 if (this.outputs.length > 0){
472 472 // have at least one output to consider
473 473 var last = this.outputs[this.outputs.length-1];
474 474 if (last.output_type == 'stream' && json.stream == last.stream){
475 475 // latest output was in the same stream,
476 476 // so append directly into its pre tag
477 477 // escape ANSI & HTML specials:
478 478 var pre = this.element.find('div.'+subclass).last().find('pre');
479 479 var html = utils.fixCarriageReturn(
480 480 pre.html() + utils.fixConsole(text));
481 481 pre.html(html);
482 482 return;
483 483 }
484 484 }
485 485
486 486 if (!text.replace("\r", "")) {
487 487 // text is nothing (empty string, \r, etc.)
488 488 // so don't append any elements, which might add undesirable space
489 489 return;
490 490 }
491 491
492 492 // If we got here, attach a new div
493 493 var toinsert = this.create_output_area();
494 494 this.append_text(text, {}, toinsert, "output_stream "+subclass);
495 495 this._safe_append(toinsert);
496 496 };
497 497
498 498
499 499 OutputArea.prototype.append_display_data = function (json, dynamic) {
500 500 var toinsert = this.create_output_area();
501 501 if (this.append_mime_type(json, toinsert, dynamic)) {
502 502 this._safe_append(toinsert);
503 503 // If we just output latex, typeset it.
504 504 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
505 505 this.typeset();
506 506 }
507 507 }
508 508 };
509 509
510 510 OutputArea.display_order = [
511 511 'application/javascript',
512 512 'text/html',
513 513 'text/latex',
514 514 'image/svg+xml',
515 515 'image/png',
516 516 'image/jpeg',
517 517 'text/plain'
518 518 ];
519 519
520 520 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
521 521
522 522 for(var type_i in OutputArea.display_order){
523 523 var type = OutputArea.display_order[type_i];
524 524 if(json[type] != undefined ){
525 525 var md = {};
526 526 if (json.metadata) {
527 527 md = json.metadata;
528 528 }
529 529 if(type == 'javascript'){
530 530 if (dynamic) {
531 531 this.append_javascript(json.javascript, md, element, dynamic);
532 532 return true;
533 533 }
534 534 } else {
535 535 var old_name = OutputArea.mime_map[type]
536 536 this['append_'+old_name](json[type], md, element, type);
537 537 return true;
538 538 }
539 539 return false;
540 540 }
541 541 }
542 542 return false;
543 543 };
544 544
545 545
546 546 OutputArea.prototype.append_html = function (html, md, element, type) {
547 547 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
548 548 IPython.keyboard_manager.register_events(toinsert);
549 549 toinsert.append(html);
550 550 element.append(toinsert);
551 551 };
552 552
553 553
554 554 OutputArea.prototype.append_javascript = function (js, md, container, type) {
555 555 // We just eval the JS code, element appears in the local scope.
556 556 var element = this.create_output_subarea(md, "output_javascript", type);
557 557 IPython.keyboard_manager.register_events(element);
558 558 container.append(element);
559 559 try {
560 560 eval(js);
561 561 } catch(err) {
562 562 console.log(err);
563 563 this._append_javascript_error(err, element);
564 564 }
565 565 };
566 566
567 567
568 568 OutputArea.prototype.append_text = function (data, md, element, extra_class, type) {
569 569 var toinsert = this.create_output_subarea(md, "output_text", type);
570 570 // escape ANSI & HTML specials in plaintext:
571 571 data = utils.fixConsole(data);
572 572 data = utils.fixCarriageReturn(data);
573 573 data = utils.autoLinkUrls(data);
574 574 if (extra_class){
575 575 toinsert.addClass(extra_class);
576 576 }
577 577 toinsert.append($("<pre/>").html(data));
578 578 element.append(toinsert);
579 579 };
580 580
581 581
582 582 OutputArea.prototype.append_svg = function (svg, md, element, type) {
583 583 var toinsert = this.create_output_subarea(md, "output_svg", type);
584 584 toinsert.append(svg);
585 585 element.append(toinsert);
586 586 };
587 587
588 588
589 589 OutputArea.prototype._dblclick_to_reset_size = function (img) {
590 590 // schedule wrapping image in resizable after a delay,
591 591 // so we don't end up calling resize on a zero-size object
592 592 var that = this;
593 593 setTimeout(function () {
594 594 var h0 = img.height();
595 595 var w0 = img.width();
596 596 if (!(h0 && w0)) {
597 597 // zero size, schedule another timeout
598 598 that._dblclick_to_reset_size(img);
599 599 return;
600 600 }
601 601 img.resizable({
602 602 aspectRatio: true,
603 603 autoHide: true
604 604 });
605 605 img.dblclick(function () {
606 606 // resize wrapper & image together for some reason:
607 607 img.parent().height(h0);
608 608 img.height(h0);
609 609 img.parent().width(w0);
610 610 img.width(w0);
611 611 });
612 612 }, 250);
613 613 };
614 614
615 615
616 616 OutputArea.prototype.append_png = function (png, md, element, type) {
617 617 var toinsert = this.create_output_subarea(md, "output_png", type);
618 618 var img = $("<img/>");
619 img[0].attr('src','data:image/png;base64,'+png);
619 img[0].setAttribute('src','data:image/png;base64,'+png);
620 620 if (md['height']) {
621 621 img[0].setAttribute('height', md['height']);
622 622 }
623 623 if (md['width']) {
624 624 img[0].setAttribute('width', md['width']);
625 625 }
626 626 this._dblclick_to_reset_size(img);
627 627 toinsert.append(img);
628 628 element.append(toinsert);
629 629 };
630 630
631 631
632 632 OutputArea.prototype.append_jpeg = function (jpeg, md, element, type) {
633 633 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
634 634 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
635 635 if (md['height']) {
636 636 img.attr('height', md['height']);
637 637 }
638 638 if (md['width']) {
639 639 img.attr('width', md['width']);
640 640 }
641 641 this._dblclick_to_reset_size(img);
642 642 toinsert.append(img);
643 643 element.append(toinsert);
644 644 };
645 645
646 646
647 647 OutputArea.prototype.append_latex = function (latex, md, element, type) {
648 648 // This method cannot do the typesetting because the latex first has to
649 649 // be on the page.
650 650 var toinsert = this.create_output_subarea(md, "output_latex", type);
651 651 toinsert.append(latex);
652 652 element.append(toinsert);
653 653 };
654 654
655 655 OutputArea.prototype.append_raw_input = function (msg) {
656 656 var that = this;
657 657 this.expand();
658 658 var content = msg.content;
659 659 var area = this.create_output_area();
660 660
661 661 // disable any other raw_inputs, if they are left around
662 662 $("div.output_subarea.raw_input").remove();
663 663
664 664 area.append(
665 665 $("<div/>")
666 666 .addClass("box-flex1 output_subarea raw_input")
667 667 .append(
668 668 $("<span/>")
669 669 .addClass("input_prompt")
670 670 .text(content.prompt)
671 671 )
672 672 .append(
673 673 $("<input/>")
674 674 .addClass("raw_input")
675 675 .attr('type', 'text')
676 676 .attr("size", 47)
677 677 .keydown(function (event, ui) {
678 678 // make sure we submit on enter,
679 679 // and don't re-execute the *cell* on shift-enter
680 680 if (event.which === utils.keycodes.ENTER) {
681 681 that._submit_raw_input();
682 682 return false;
683 683 }
684 684 })
685 685 )
686 686 );
687 687
688 688 this.element.append(area);
689 689 var raw_input = area.find('input.raw_input');
690 690 // Register events that enable/disable the keyboard manager while raw
691 691 // input is focused.
692 692 IPython.keyboard_manager.register_events(raw_input);
693 693 // Note, the following line used to read raw_input.focus().focus().
694 694 // This seemed to be needed otherwise only the cell would be focused.
695 695 // But with the modal UI, this seems to work fine with one call to focus().
696 696 raw_input.focus();
697 697 }
698 698
699 699 OutputArea.prototype._submit_raw_input = function (evt) {
700 700 var container = this.element.find("div.raw_input");
701 701 var theprompt = container.find("span.input_prompt");
702 702 var theinput = container.find("input.raw_input");
703 703 var value = theinput.val();
704 704 var content = {
705 705 output_type : 'stream',
706 706 name : 'stdout',
707 707 text : theprompt.text() + value + '\n'
708 708 }
709 709 // remove form container
710 710 container.parent().remove();
711 711 // replace with plaintext version in stdout
712 712 this.append_output(content, false);
713 713 $([IPython.events]).trigger('send_input_reply.Kernel', value);
714 714 }
715 715
716 716
717 717 OutputArea.prototype.handle_clear_output = function (msg) {
718 718 this.clear_output(msg.content.wait);
719 719 };
720 720
721 721
722 722 OutputArea.prototype.clear_output = function(wait) {
723 723 if (wait) {
724 724
725 725 // If a clear is queued, clear before adding another to the queue.
726 726 if (this.clear_queued) {
727 727 this.clear_output(false);
728 728 };
729 729
730 730 this.clear_queued = true;
731 731 } else {
732 732
733 733 // Fix the output div's height if the clear_output is waiting for
734 734 // new output (it is being used in an animation).
735 735 if (this.clear_queued) {
736 736 var height = this.element.height();
737 737 this.element.height(height);
738 738 this.clear_queued = false;
739 739 }
740 740
741 741 // clear all, no need for logic
742 742 this.element.html("");
743 743 this.outputs = [];
744 744 this.unscroll_area();
745 745 return;
746 746 };
747 747 };
748 748
749 749
750 750 // JSON serialization
751 751
752 752 OutputArea.prototype.fromJSON = function (outputs) {
753 753 // add here the mapping of short key to mime type
754 754 // TODO: remove this when we update to nbformat 4
755 755 var len = outputs.length;
756 756 for (var i=0; i<len; i++) {
757 757 // convert mime keys
758 758 var data = outputs[i];
759 759 var msg_type = data.output_type;
760 760 if (msg_type === "display_data" || msg_type === "pyout") {
761 761 this.convert_mime_types_r(data);
762 762 this.convert_mime_types_r(data.metadata);
763 763 }
764 764
765 765 // append with dynamic=false.
766 766 this.append_output(data, false);
767 767 }
768 768 };
769 769
770 770
771 771 OutputArea.prototype.toJSON = function () {
772 772 var outputs = [];
773 773 var len = this.outputs.length;
774 774 for (var i=0; i<len; i++) {
775 775 outputs[i] = this.outputs[i];
776 776 }
777 777 return outputs;
778 778 };
779 779
780 780
781 781 IPython.OutputArea = OutputArea;
782 782
783 783 return IPython;
784 784
785 785 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now