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