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