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