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