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