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