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