Show More
@@ -1,832 +1,843 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // OutputArea |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule OutputArea |
|
16 | 16 | */ |
|
17 | 17 | var IPython = (function (IPython) { |
|
18 | 18 | "use strict"; |
|
19 | 19 | |
|
20 | 20 | var utils = IPython.utils; |
|
21 | 21 | |
|
22 | 22 | /** |
|
23 | 23 | * @class OutputArea |
|
24 | 24 | * |
|
25 | 25 | * @constructor |
|
26 | 26 | */ |
|
27 | 27 | |
|
28 | 28 | var OutputArea = function (selector, prompt_area) { |
|
29 | 29 | this.selector = selector; |
|
30 | 30 | this.wrapper = $(selector); |
|
31 | 31 | this.outputs = []; |
|
32 | 32 | this.collapsed = false; |
|
33 | 33 | this.scrolled = false; |
|
34 | 34 | this.clear_queued = null; |
|
35 | 35 | if (prompt_area === undefined) { |
|
36 | 36 | this.prompt_area = true; |
|
37 | 37 | } else { |
|
38 | 38 | this.prompt_area = prompt_area; |
|
39 | 39 | } |
|
40 | 40 | this.create_elements(); |
|
41 | 41 | this.style(); |
|
42 | 42 | this.bind_events(); |
|
43 | 43 | }; |
|
44 | ||
|
44 | ||
|
45 | /** | |
|
46 | * Class properties | |
|
47 | **/ | |
|
48 | ||
|
49 | /** | |
|
50 | * Threshold to trigger autoscroll when the OutputArea is resized, | |
|
51 | * typically when new outputs are added. | |
|
52 | * | |
|
53 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, | |
|
54 | * unless it is < 0, in which case autoscroll will never be triggered | |
|
55 | * | |
|
56 | * @property auto_scroll_threshold | |
|
57 | * @type Number | |
|
58 | * @default 100 | |
|
59 | * | |
|
60 | **/ | |
|
61 | OutputArea.auto_scroll_threshold = 100; | |
|
62 | ||
|
63 | /** | |
|
64 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | |
|
65 | * shorter than this are never scrolled. | |
|
66 | * | |
|
67 | * @property minimum_scroll_threshold | |
|
68 | * @type Number | |
|
69 | * @default 20 | |
|
70 | * | |
|
71 | **/ | |
|
72 | OutputArea.minimum_scroll_threshold = 20; | |
|
73 | ||
|
74 | ||
|
75 | ||
|
76 | OutputArea.mime_map = { | |
|
77 | "text/plain" : "text", | |
|
78 | "text/html" : "html", | |
|
79 | "image/svg+xml" : "svg", | |
|
80 | "image/png" : "png", | |
|
81 | "image/jpeg" : "jpeg", | |
|
82 | "text/latex" : "latex", | |
|
83 | "application/json" : "json", | |
|
84 | "application/javascript" : "javascript", | |
|
85 | }; | |
|
86 | ||
|
87 | OutputArea.mime_map_r = { | |
|
88 | "text" : "text/plain", | |
|
89 | "html" : "text/html", | |
|
90 | "svg" : "image/svg+xml", | |
|
91 | "png" : "image/png", | |
|
92 | "jpeg" : "image/jpeg", | |
|
93 | "latex" : "text/latex", | |
|
94 | "json" : "application/json", | |
|
95 | "javascript" : "application/javascript", | |
|
96 | }; | |
|
97 | ||
|
98 | OutputArea.display_order = [ | |
|
99 | 'application/javascript', | |
|
100 | 'text/html', | |
|
101 | 'text/latex', | |
|
102 | 'image/svg+xml', | |
|
103 | 'image/png', | |
|
104 | 'image/jpeg', | |
|
105 | 'text/plain' | |
|
106 | ]; | |
|
107 | ||
|
108 | OutputArea.append_map = { | |
|
109 | "text/plain" : OutputArea.prototype.append_text, | |
|
110 | "text/html" : OutputArea.prototype.append_html, | |
|
111 | "image/svg+xml" : OutputArea.prototype.append_svg, | |
|
112 | "image/png" : OutputArea.prototype.append_png, | |
|
113 | "image/jpeg" : OutputArea.prototype.append_jpeg, | |
|
114 | "text/latex" : OutputArea.prototype.append_latex, | |
|
115 | "application/json" : OutputArea.prototype.append_json, | |
|
116 | "application/javascript" : OutputArea.prototype.append_javascript, | |
|
117 | }; | |
|
118 | ||
|
119 | /** | |
|
120 | * Class prototypes | |
|
121 | **/ | |
|
122 | ||
|
45 | 123 | OutputArea.prototype.create_elements = function () { |
|
46 | 124 | this.element = $("<div/>"); |
|
47 | 125 | this.collapse_button = $("<div/>"); |
|
48 | 126 | this.prompt_overlay = $("<div/>"); |
|
49 | 127 | this.wrapper.append(this.prompt_overlay); |
|
50 | 128 | this.wrapper.append(this.element); |
|
51 | 129 | this.wrapper.append(this.collapse_button); |
|
52 | 130 | }; |
|
53 | 131 | |
|
54 | 132 | |
|
55 | 133 | OutputArea.prototype.style = function () { |
|
56 | 134 | this.collapse_button.hide(); |
|
57 | 135 | this.prompt_overlay.hide(); |
|
58 | 136 | |
|
59 | 137 | this.wrapper.addClass('output_wrapper'); |
|
60 | 138 | this.element.addClass('output'); |
|
61 | 139 | |
|
62 | 140 | this.collapse_button.addClass("btn output_collapsed"); |
|
63 | 141 | this.collapse_button.attr('title', 'click to expand output'); |
|
64 | 142 | this.collapse_button.text('. . .'); |
|
65 | 143 | |
|
66 | 144 | this.prompt_overlay.addClass('out_prompt_overlay prompt'); |
|
67 | 145 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); |
|
68 | 146 | |
|
69 | 147 | this.collapse(); |
|
70 | 148 | }; |
|
71 | 149 | |
|
72 | 150 | /** |
|
73 | 151 | * Should the OutputArea scroll? |
|
74 | 152 | * Returns whether the height (in lines) exceeds a threshold. |
|
75 | 153 | * |
|
76 | 154 | * @private |
|
77 | 155 | * @method _should_scroll |
|
78 | 156 | * @param [lines=100]{Integer} |
|
79 | 157 | * @return {Bool} |
|
80 | 158 | * |
|
81 | 159 | */ |
|
82 | 160 | OutputArea.prototype._should_scroll = function (lines) { |
|
83 | 161 | if (lines <=0 ){ return } |
|
84 | 162 | if (!lines) { |
|
85 | 163 | lines = 100; |
|
86 | 164 | } |
|
87 | 165 | // line-height from http://stackoverflow.com/questions/1185151 |
|
88 | 166 | var fontSize = this.element.css('font-size'); |
|
89 | 167 | var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); |
|
90 | 168 | |
|
91 | 169 | return (this.element.height() > lines * lineHeight); |
|
92 | 170 | }; |
|
93 | 171 | |
|
94 | 172 | |
|
95 | 173 | OutputArea.prototype.bind_events = function () { |
|
96 | 174 | var that = this; |
|
97 | 175 | this.prompt_overlay.dblclick(function () { that.toggle_output(); }); |
|
98 | 176 | this.prompt_overlay.click(function () { that.toggle_scroll(); }); |
|
99 | 177 | |
|
100 | 178 | this.element.resize(function () { |
|
101 | 179 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled |
|
102 | 180 | if ( IPython.utils.browser[0] === "Firefox" ) { |
|
103 | 181 | return; |
|
104 | 182 | } |
|
105 | 183 | // maybe scroll output, |
|
106 | 184 | // if it's grown large enough and hasn't already been scrolled. |
|
107 | 185 | if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) { |
|
108 | 186 | that.scroll_area(); |
|
109 | 187 | } |
|
110 | 188 | }); |
|
111 | 189 | this.collapse_button.click(function () { |
|
112 | 190 | that.expand(); |
|
113 | 191 | }); |
|
114 | 192 | }; |
|
115 | 193 | |
|
116 | 194 | |
|
117 | 195 | OutputArea.prototype.collapse = function () { |
|
118 | 196 | if (!this.collapsed) { |
|
119 | 197 | this.element.hide(); |
|
120 | 198 | this.prompt_overlay.hide(); |
|
121 | 199 | if (this.element.html()){ |
|
122 | 200 | this.collapse_button.show(); |
|
123 | 201 | } |
|
124 | 202 | this.collapsed = true; |
|
125 | 203 | } |
|
126 | 204 | }; |
|
127 | 205 | |
|
128 | 206 | |
|
129 | 207 | OutputArea.prototype.expand = function () { |
|
130 | 208 | if (this.collapsed) { |
|
131 | 209 | this.collapse_button.hide(); |
|
132 | 210 | this.element.show(); |
|
133 | 211 | this.prompt_overlay.show(); |
|
134 | 212 | this.collapsed = false; |
|
135 | 213 | } |
|
136 | 214 | }; |
|
137 | 215 | |
|
138 | 216 | |
|
139 | 217 | OutputArea.prototype.toggle_output = function () { |
|
140 | 218 | if (this.collapsed) { |
|
141 | 219 | this.expand(); |
|
142 | 220 | } else { |
|
143 | 221 | this.collapse(); |
|
144 | 222 | } |
|
145 | 223 | }; |
|
146 | 224 | |
|
147 | 225 | |
|
148 | 226 | OutputArea.prototype.scroll_area = function () { |
|
149 | 227 | this.element.addClass('output_scroll'); |
|
150 | 228 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); |
|
151 | 229 | this.scrolled = true; |
|
152 | 230 | }; |
|
153 | 231 | |
|
154 | 232 | |
|
155 | 233 | OutputArea.prototype.unscroll_area = function () { |
|
156 | 234 | this.element.removeClass('output_scroll'); |
|
157 | 235 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); |
|
158 | 236 | this.scrolled = false; |
|
159 | 237 | }; |
|
160 | 238 | |
|
161 | 239 | /** |
|
162 | * Threshold to trigger autoscroll when the OutputArea is resized, | |
|
163 | * typically when new outputs are added. | |
|
164 | * | |
|
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 | |
|
167 | * | |
|
168 | * @property auto_scroll_threshold | |
|
169 | * @type Number | |
|
170 | * @default 100 | |
|
171 | * | |
|
172 | **/ | |
|
173 | OutputArea.auto_scroll_threshold = 100; | |
|
174 | ||
|
175 | ||
|
176 | /** | |
|
177 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | |
|
178 | * shorter than this are never scrolled. | |
|
179 | * | |
|
180 | * @property minimum_scroll_threshold | |
|
181 | * @type Number | |
|
182 | * @default 20 | |
|
183 | * | |
|
184 | **/ | |
|
185 | OutputArea.minimum_scroll_threshold = 20; | |
|
186 | ||
|
187 | ||
|
188 | /** | |
|
189 | 240 | * |
|
190 | 241 | * Scroll OutputArea if height supperior than a threshold (in lines). |
|
191 | 242 | * |
|
192 | 243 | * Threshold is a maximum number of lines. If unspecified, defaults to |
|
193 | 244 | * OutputArea.minimum_scroll_threshold. |
|
194 | 245 | * |
|
195 | 246 | * Negative threshold will prevent the OutputArea from ever scrolling. |
|
196 | 247 | * |
|
197 | 248 | * @method scroll_if_long |
|
198 | 249 | * |
|
199 | 250 | * @param [lines=20]{Number} Default to 20 if not set, |
|
200 | 251 | * behavior undefined for value of `0`. |
|
201 | 252 | * |
|
202 | 253 | **/ |
|
203 | 254 | OutputArea.prototype.scroll_if_long = function (lines) { |
|
204 | 255 | var n = lines | OutputArea.minimum_scroll_threshold; |
|
205 | 256 | if(n <= 0){ |
|
206 | 257 | return |
|
207 | 258 | } |
|
208 | 259 | |
|
209 | 260 | if (this._should_scroll(n)) { |
|
210 | 261 | // only allow scrolling long-enough output |
|
211 | 262 | this.scroll_area(); |
|
212 | 263 | } |
|
213 | 264 | }; |
|
214 | 265 | |
|
215 | 266 | |
|
216 | 267 | OutputArea.prototype.toggle_scroll = function () { |
|
217 | 268 | if (this.scrolled) { |
|
218 | 269 | this.unscroll_area(); |
|
219 | 270 | } else { |
|
220 | 271 | // only allow scrolling long-enough output |
|
221 | 272 | this.scroll_if_long(); |
|
222 | 273 | } |
|
223 | 274 | }; |
|
224 | 275 | |
|
225 | 276 | |
|
226 | 277 | // typeset with MathJax if MathJax is available |
|
227 | 278 | OutputArea.prototype.typeset = function () { |
|
228 | 279 | if (window.MathJax){ |
|
229 | 280 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
230 | 281 | } |
|
231 | 282 | }; |
|
232 | 283 | |
|
233 | 284 | |
|
234 | 285 | OutputArea.prototype.handle_output = function (msg) { |
|
235 | 286 | var json = {}; |
|
236 | 287 | var msg_type = json.output_type = msg.header.msg_type; |
|
237 | 288 | var content = msg.content; |
|
238 | 289 | if (msg_type === "stream") { |
|
239 | 290 | json.text = content.data; |
|
240 | 291 | json.stream = content.name; |
|
241 | 292 | } else if (msg_type === "display_data") { |
|
242 | 293 | json = content.data; |
|
243 | 294 | json.output_type = msg_type; |
|
244 | 295 | json.metadata = content.metadata; |
|
245 | 296 | } else if (msg_type === "pyout") { |
|
246 | 297 | json = content.data; |
|
247 | 298 | json.output_type = msg_type; |
|
248 | 299 | json.metadata = content.metadata; |
|
249 | 300 | json.prompt_number = content.execution_count; |
|
250 | 301 | } else if (msg_type === "pyerr") { |
|
251 | 302 | json.ename = content.ename; |
|
252 | 303 | json.evalue = content.evalue; |
|
253 | 304 | json.traceback = content.traceback; |
|
254 | 305 | } |
|
255 | 306 | this.append_output(json); |
|
256 | 307 | }; |
|
257 | ||
|
258 | OutputArea.mime_map = { | |
|
259 | "text/plain" : "text", | |
|
260 | "text/html" : "html", | |
|
261 | "image/svg+xml" : "svg", | |
|
262 | "image/png" : "png", | |
|
263 | "image/jpeg" : "jpeg", | |
|
264 | "text/latex" : "latex", | |
|
265 | "application/json" : "json", | |
|
266 | "application/javascript" : "javascript", | |
|
267 | }; | |
|
268 | 308 | |
|
269 | OutputArea.mime_map_r = { | |
|
270 | "text" : "text/plain", | |
|
271 | "html" : "text/html", | |
|
272 | "svg" : "image/svg+xml", | |
|
273 | "png" : "image/png", | |
|
274 | "jpeg" : "image/jpeg", | |
|
275 | "latex" : "text/latex", | |
|
276 | "json" : "application/json", | |
|
277 | "javascript" : "application/javascript", | |
|
278 | }; | |
|
279 | 309 | |
|
280 | 310 | OutputArea.prototype.rename_keys = function (data, key_map) { |
|
281 | 311 | var remapped = {}; |
|
282 | 312 | for (var key in data) { |
|
283 | 313 | var new_key = key_map[key] || key; |
|
284 | 314 | remapped[new_key] = data[key]; |
|
285 | 315 | } |
|
286 | 316 | return remapped; |
|
287 | 317 | }; |
|
288 | 318 | |
|
289 | 319 | |
|
290 | 320 | OutputArea.output_types = [ |
|
291 | 321 | 'application/javascript', |
|
292 | 322 | 'text/html', |
|
293 | 323 | 'text/latex', |
|
294 | 324 | 'image/svg+xml', |
|
295 | 325 | 'image/png', |
|
296 | 326 | 'image/jpeg', |
|
297 | 327 | 'text/plain' |
|
298 | 328 | ]; |
|
299 | 329 | |
|
300 | 330 | OutputArea.prototype.validate_output = function (json) { |
|
301 | 331 | // scrub invalid outputs |
|
302 | 332 | // TODO: right now everything is a string, but JSON really shouldn't be. |
|
303 | 333 | // nbformat 4 will fix that. |
|
304 | 334 | $.map(OutputArea.output_types, function(key){ |
|
305 | 335 | if (json[key] !== undefined && typeof json[key] !== 'string') { |
|
306 | 336 | console.log("Invalid type for " + key, json[key]); |
|
307 | 337 | delete json[key]; |
|
308 | 338 | } |
|
309 | 339 | }); |
|
310 | 340 | return json; |
|
311 | 341 | }; |
|
312 | 342 | |
|
313 | 343 | OutputArea.prototype.append_output = function (json) { |
|
314 | 344 | this.expand(); |
|
315 | 345 | // Clear the output if clear is queued. |
|
316 | 346 | var needs_height_reset = false; |
|
317 | 347 | if (this.clear_queued) { |
|
318 | 348 | this.clear_output(false); |
|
319 | 349 | needs_height_reset = true; |
|
320 | 350 | } |
|
321 | 351 | |
|
322 | 352 | // validate output data types |
|
323 | 353 | json = this.validate_output(json); |
|
324 | 354 | |
|
325 | 355 | if (json.output_type === 'pyout') { |
|
326 | 356 | this.append_pyout(json); |
|
327 | 357 | } else if (json.output_type === 'pyerr') { |
|
328 | 358 | this.append_pyerr(json); |
|
329 | 359 | } else if (json.output_type === 'display_data') { |
|
330 | 360 | this.append_display_data(json); |
|
331 | 361 | } else if (json.output_type === 'stream') { |
|
332 | 362 | this.append_stream(json); |
|
333 | 363 | } |
|
334 | 364 | this.outputs.push(json); |
|
335 | 365 | |
|
336 | 366 | // Only reset the height to automatic if the height is currently |
|
337 | 367 | // fixed (done by wait=True flag on clear_output). |
|
338 | 368 | if (needs_height_reset) { |
|
339 | 369 | this.element.height(''); |
|
340 | 370 | } |
|
341 | 371 | |
|
342 | 372 | var that = this; |
|
343 | 373 | setTimeout(function(){that.element.trigger('resize');}, 100); |
|
344 | 374 | }; |
|
345 | 375 | |
|
346 | 376 | |
|
347 | 377 | OutputArea.prototype.create_output_area = function () { |
|
348 | 378 | var oa = $("<div/>").addClass("output_area"); |
|
349 | 379 | if (this.prompt_area) { |
|
350 | 380 | oa.append($('<div/>').addClass('prompt')); |
|
351 | 381 | } |
|
352 | 382 | return oa; |
|
353 | 383 | }; |
|
354 | 384 | |
|
355 | 385 | |
|
356 | 386 | function _get_metadata_key(metadata, key, mime) { |
|
357 | 387 | var mime_md = metadata[mime]; |
|
358 | 388 | // mime-specific higher priority |
|
359 | 389 | if (mime_md && mime_md[key] !== undefined) { |
|
360 | 390 | return mime_md[key]; |
|
361 | 391 | } |
|
362 | 392 | // fallback on global |
|
363 | 393 | return metadata[key]; |
|
364 | 394 | } |
|
365 | 395 | |
|
366 | 396 | OutputArea.prototype.create_output_subarea = function(md, classes, mime) { |
|
367 | 397 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); |
|
368 | 398 | if (_get_metadata_key(md, 'isolated', mime)) { |
|
369 | 399 | // Create an iframe to isolate the subarea from the rest of the |
|
370 | 400 | // document |
|
371 | 401 | var iframe = $('<iframe/>').addClass('box-flex1'); |
|
372 | 402 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); |
|
373 | 403 | iframe.attr('frameborder', 0); |
|
374 | 404 | iframe.attr('scrolling', 'auto'); |
|
375 | 405 | |
|
376 | 406 | // Once the iframe is loaded, the subarea is dynamically inserted |
|
377 | 407 | iframe.on('load', function() { |
|
378 | 408 | // Workaround needed by Firefox, to properly render svg inside |
|
379 | 409 | // iframes, see http://stackoverflow.com/questions/10177190/ |
|
380 | 410 | // svg-dynamically-added-to-iframe-does-not-render-correctly |
|
381 | 411 | this.contentDocument.open(); |
|
382 | 412 | |
|
383 | 413 | // Insert the subarea into the iframe |
|
384 | 414 | // We must directly write the html. When using Jquery's append |
|
385 | 415 | // method, javascript is evaluated in the parent document and |
|
386 | 416 | // not in the iframe document. |
|
387 | 417 | this.contentDocument.write(subarea.html()); |
|
388 | 418 | |
|
389 | 419 | this.contentDocument.close(); |
|
390 | 420 | |
|
391 | 421 | var body = this.contentDocument.body; |
|
392 | 422 | // Adjust the iframe height automatically |
|
393 | 423 | iframe.height(body.scrollHeight + 'px'); |
|
394 | 424 | }); |
|
395 | 425 | |
|
396 | 426 | // Elements should be appended to the inner subarea and not to the |
|
397 | 427 | // iframe |
|
398 | 428 | iframe.append = function(that) { |
|
399 | 429 | subarea.append(that); |
|
400 | 430 | }; |
|
401 | 431 | |
|
402 | 432 | return iframe; |
|
403 | 433 | } else { |
|
404 | 434 | return subarea; |
|
405 | 435 | } |
|
406 | 436 | } |
|
407 | 437 | |
|
408 | 438 | |
|
409 | 439 | OutputArea.prototype._append_javascript_error = function (err, element) { |
|
410 | 440 | // display a message when a javascript error occurs in display output |
|
411 | 441 | var msg = "Javascript error adding output!" |
|
412 | 442 | if ( element === undefined ) return; |
|
413 | 443 | element.append( |
|
414 | 444 | $('<div/>').html(msg + "<br/>" + |
|
415 | 445 | err.toString() + |
|
416 | 446 | '<br/>See your browser Javascript console for more details.' |
|
417 | 447 | ).addClass('js-error') |
|
418 | 448 | ); |
|
419 | 449 | }; |
|
420 | 450 | |
|
421 | 451 | OutputArea.prototype._safe_append = function (toinsert) { |
|
422 | 452 | // safely append an item to the document |
|
423 | 453 | // this is an object created by user code, |
|
424 | 454 | // and may have errors, which should not be raised |
|
425 | 455 | // under any circumstances. |
|
426 | 456 | try { |
|
427 | 457 | this.element.append(toinsert); |
|
428 | 458 | } catch(err) { |
|
429 | 459 | console.log(err); |
|
430 | 460 | // Create an actual output_area and output_subarea, which creates |
|
431 | 461 | // the prompt area and the proper indentation. |
|
432 | 462 | var toinsert = this.create_output_area(); |
|
433 | 463 | var subarea = $('<div/>').addClass('output_subarea'); |
|
434 | 464 | toinsert.append(subarea); |
|
435 | 465 | this._append_javascript_error(err, subarea); |
|
436 | 466 | this.element.append(toinsert); |
|
437 | 467 | } |
|
438 | 468 | }; |
|
439 | 469 | |
|
440 | 470 | |
|
441 | 471 | OutputArea.prototype.append_pyout = function (json) { |
|
442 | 472 | var n = json.prompt_number || ' '; |
|
443 | 473 | var toinsert = this.create_output_area(); |
|
444 | 474 | if (this.prompt_area) { |
|
445 | 475 | toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:'); |
|
446 | 476 | } |
|
447 | 477 | this.append_mime_type(json, toinsert); |
|
448 | 478 | this._safe_append(toinsert); |
|
449 | 479 | // If we just output latex, typeset it. |
|
450 | 480 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
451 | 481 | this.typeset(); |
|
452 | 482 | } |
|
453 | 483 | }; |
|
454 | 484 | |
|
455 | 485 | |
|
456 | 486 | OutputArea.prototype.append_pyerr = function (json) { |
|
457 | 487 | var tb = json.traceback; |
|
458 | 488 | if (tb !== undefined && tb.length > 0) { |
|
459 | 489 | var s = ''; |
|
460 | 490 | var len = tb.length; |
|
461 | 491 | for (var i=0; i<len; i++) { |
|
462 | 492 | s = s + tb[i] + '\n'; |
|
463 | 493 | } |
|
464 | 494 | s = s + '\n'; |
|
465 | 495 | var toinsert = this.create_output_area(); |
|
466 | 496 | this.append_text(s, {}, toinsert); |
|
467 | 497 | this._safe_append(toinsert); |
|
468 | 498 | } |
|
469 | 499 | }; |
|
470 | 500 | |
|
471 | 501 | |
|
472 | 502 | OutputArea.prototype.append_stream = function (json) { |
|
473 | 503 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
474 | 504 | // default to most likely stdout: |
|
475 | 505 | if (json.stream == undefined){ |
|
476 | 506 | json.stream = 'stdout'; |
|
477 | 507 | } |
|
478 | 508 | var text = json.text; |
|
479 | 509 | var subclass = "output_"+json.stream; |
|
480 | 510 | if (this.outputs.length > 0){ |
|
481 | 511 | // have at least one output to consider |
|
482 | 512 | var last = this.outputs[this.outputs.length-1]; |
|
483 | 513 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
484 | 514 | // latest output was in the same stream, |
|
485 | 515 | // so append directly into its pre tag |
|
486 | 516 | // escape ANSI & HTML specials: |
|
487 | 517 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
488 | 518 | var html = utils.fixCarriageReturn( |
|
489 | 519 | pre.html() + utils.fixConsole(text)); |
|
490 | 520 | pre.html(html); |
|
491 | 521 | return; |
|
492 | 522 | } |
|
493 | 523 | } |
|
494 | 524 | |
|
495 | 525 | if (!text.replace("\r", "")) { |
|
496 | 526 | // text is nothing (empty string, \r, etc.) |
|
497 | 527 | // so don't append any elements, which might add undesirable space |
|
498 | 528 | return; |
|
499 | 529 | } |
|
500 | 530 | |
|
501 | 531 | // If we got here, attach a new div |
|
502 | 532 | var toinsert = this.create_output_area(); |
|
503 | 533 | this.append_text(text, {}, toinsert, "output_stream "+subclass); |
|
504 | 534 | this._safe_append(toinsert); |
|
505 | 535 | }; |
|
506 | 536 | |
|
507 | 537 | |
|
508 | 538 | OutputArea.prototype.append_display_data = function (json) { |
|
509 | 539 | var toinsert = this.create_output_area(); |
|
510 | 540 | if (this.append_mime_type(json, toinsert)) { |
|
511 | 541 | this._safe_append(toinsert); |
|
512 | 542 | // If we just output latex, typeset it. |
|
513 | 543 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
514 | 544 | this.typeset(); |
|
515 | 545 | } |
|
516 | 546 | } |
|
517 | 547 | }; |
|
518 | 548 | |
|
519 | OutputArea.display_order = [ | |
|
520 | 'application/javascript', | |
|
521 | 'text/html', | |
|
522 | 'text/latex', | |
|
523 | 'image/svg+xml', | |
|
524 | 'image/png', | |
|
525 | 'image/jpeg', | |
|
526 | 'text/plain' | |
|
527 | ]; | |
|
528 | 549 | |
|
529 | 550 | OutputArea.prototype.append_mime_type = function (json, element) { |
|
530 | 551 | |
|
531 | 552 | for (var type_i in OutputArea.display_order) { |
|
532 | 553 | var type = OutputArea.display_order[type_i]; |
|
533 | 554 | var append = OutputArea.append_map[type]; |
|
534 | 555 | if ((json[type] !== undefined) && append) { |
|
535 | 556 | var md = json.metadata || {}; |
|
536 | 557 | var toinsert = append.apply(this, [json[type], md, element]); |
|
537 | 558 | $([IPython.events]).trigger('output_appended.OutputArea', [type, json[type], md, toinsert]); |
|
538 | 559 | return true; |
|
539 | 560 | } |
|
540 | 561 | } |
|
541 | 562 | return false; |
|
542 | 563 | }; |
|
543 | 564 | |
|
544 | 565 | |
|
545 | 566 | OutputArea.prototype.append_html = function (html, md, element) { |
|
546 | 567 | var type = 'text/html'; |
|
547 | 568 | var toinsert = this.create_output_subarea(md, "output_html rendered_html", type); |
|
548 | 569 | IPython.keyboard_manager.register_events(toinsert); |
|
549 | 570 | toinsert.append(html); |
|
550 | 571 | element.append(toinsert); |
|
551 | 572 | return toinsert; |
|
552 | 573 | }; |
|
553 | 574 | |
|
554 | 575 | |
|
555 | 576 | OutputArea.prototype.append_javascript = function (js, md, element) { |
|
556 | 577 | // We just eval the JS code, element appears in the local scope. |
|
557 | 578 | var type = 'application/javascript'; |
|
558 | 579 | var toinsert = this.create_output_subarea(md, "output_javascript", type); |
|
559 | 580 | IPython.keyboard_manager.register_events(toinsert); |
|
560 | 581 | element.append(toinsert); |
|
561 | 582 | // FIXME TODO : remove `container element for 3.0` |
|
562 | 583 | //backward compat, js should be eval'ed in a context where `container` is defined. |
|
563 | 584 | var container = element; |
|
564 | 585 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; |
|
565 | 586 | // end backward compat |
|
566 | 587 | try { |
|
567 | 588 | eval(js); |
|
568 | 589 | } catch(err) { |
|
569 | 590 | console.log(err); |
|
570 | 591 | this._append_javascript_error(err, toinsert); |
|
571 | 592 | } |
|
572 | 593 | return toinsert; |
|
573 | 594 | }; |
|
574 | 595 | |
|
575 | 596 | |
|
576 | 597 | OutputArea.prototype.append_text = function (data, md, element, extra_class) { |
|
577 | 598 | var type = 'text/plain'; |
|
578 | 599 | var toinsert = this.create_output_subarea(md, "output_text", type); |
|
579 | 600 | // escape ANSI & HTML specials in plaintext: |
|
580 | 601 | data = utils.fixConsole(data); |
|
581 | 602 | data = utils.fixCarriageReturn(data); |
|
582 | 603 | data = utils.autoLinkUrls(data); |
|
583 | 604 | if (extra_class){ |
|
584 | 605 | toinsert.addClass(extra_class); |
|
585 | 606 | } |
|
586 | 607 | toinsert.append($("<pre/>").html(data)); |
|
587 | 608 | element.append(toinsert); |
|
588 | 609 | return toinsert; |
|
589 | 610 | }; |
|
590 | 611 | |
|
591 | 612 | |
|
592 | 613 | OutputArea.prototype.append_svg = function (svg, md, element) { |
|
593 | 614 | var type = 'image/svg+xml'; |
|
594 | 615 | var toinsert = this.create_output_subarea(md, "output_svg", type); |
|
595 | 616 | toinsert.append(svg); |
|
596 | 617 | element.append(toinsert); |
|
597 | 618 | return toinsert; |
|
598 | 619 | }; |
|
599 | 620 | |
|
600 | 621 | |
|
601 | 622 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
602 | 623 | // schedule wrapping image in resizable after a delay, |
|
603 | 624 | // so we don't end up calling resize on a zero-size object |
|
604 | 625 | var that = this; |
|
605 | 626 | setTimeout(function () { |
|
606 | 627 | var h0 = img.height(); |
|
607 | 628 | var w0 = img.width(); |
|
608 | 629 | if (!(h0 && w0)) { |
|
609 | 630 | // zero size, schedule another timeout |
|
610 | 631 | that._dblclick_to_reset_size(img); |
|
611 | 632 | return; |
|
612 | 633 | } |
|
613 | 634 | img.resizable({ |
|
614 | 635 | aspectRatio: true, |
|
615 | 636 | autoHide: true |
|
616 | 637 | }); |
|
617 | 638 | img.dblclick(function () { |
|
618 | 639 | // resize wrapper & image together for some reason: |
|
619 | 640 | img.parent().height(h0); |
|
620 | 641 | img.height(h0); |
|
621 | 642 | img.parent().width(w0); |
|
622 | 643 | img.width(w0); |
|
623 | 644 | }); |
|
624 | 645 | }, 250); |
|
625 | 646 | }; |
|
626 | 647 | |
|
627 | 648 | |
|
628 | 649 | OutputArea.prototype.append_png = function (png, md, element) { |
|
629 | 650 | var type = 'image/png'; |
|
630 | 651 | var toinsert = this.create_output_subarea(md, "output_png", type); |
|
631 | 652 | var img = $("<img/>"); |
|
632 | 653 | img[0].setAttribute('src','data:image/png;base64,'+png); |
|
633 | 654 | if (md['height']) { |
|
634 | 655 | img[0].setAttribute('height', md['height']); |
|
635 | 656 | } |
|
636 | 657 | if (md['width']) { |
|
637 | 658 | img[0].setAttribute('width', md['width']); |
|
638 | 659 | } |
|
639 | 660 | this._dblclick_to_reset_size(img); |
|
640 | 661 | toinsert.append(img); |
|
641 | 662 | element.append(toinsert); |
|
642 | 663 | return toinsert; |
|
643 | 664 | }; |
|
644 | 665 | |
|
645 | 666 | |
|
646 | 667 | OutputArea.prototype.append_jpeg = function (jpeg, md, element) { |
|
647 | 668 | var type = 'image/jpeg'; |
|
648 | 669 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); |
|
649 | 670 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
650 | 671 | if (md['height']) { |
|
651 | 672 | img.attr('height', md['height']); |
|
652 | 673 | } |
|
653 | 674 | if (md['width']) { |
|
654 | 675 | img.attr('width', md['width']); |
|
655 | 676 | } |
|
656 | 677 | this._dblclick_to_reset_size(img); |
|
657 | 678 | toinsert.append(img); |
|
658 | 679 | element.append(toinsert); |
|
659 | 680 | return toinsert; |
|
660 | 681 | }; |
|
661 | 682 | |
|
662 | 683 | |
|
663 | 684 | OutputArea.prototype.append_latex = function (latex, md, element) { |
|
664 | 685 | // This method cannot do the typesetting because the latex first has to |
|
665 | 686 | // be on the page. |
|
666 | 687 | var type = 'text/latex'; |
|
667 | 688 | var toinsert = this.create_output_subarea(md, "output_latex", type); |
|
668 | 689 | toinsert.append(latex); |
|
669 | 690 | element.append(toinsert); |
|
670 | 691 | return toinsert; |
|
671 | 692 | }; |
|
672 | 693 | |
|
673 | OutputArea.append_map = { | |
|
674 | "text/plain" : OutputArea.prototype.append_text, | |
|
675 | "text/html" : OutputArea.prototype.append_html, | |
|
676 | "image/svg+xml" : OutputArea.prototype.append_svg, | |
|
677 | "image/png" : OutputArea.prototype.append_png, | |
|
678 | "image/jpeg" : OutputArea.prototype.append_jpeg, | |
|
679 | "text/latex" : OutputArea.prototype.append_latex, | |
|
680 | "application/json" : OutputArea.prototype.append_json, | |
|
681 | "application/javascript" : OutputArea.prototype.append_javascript, | |
|
682 | }; | |
|
683 | 694 | |
|
684 | 695 | OutputArea.prototype.append_raw_input = function (msg) { |
|
685 | 696 | var that = this; |
|
686 | 697 | this.expand(); |
|
687 | 698 | var content = msg.content; |
|
688 | 699 | var area = this.create_output_area(); |
|
689 | 700 | |
|
690 | 701 | // disable any other raw_inputs, if they are left around |
|
691 | 702 | $("div.output_subarea.raw_input").remove(); |
|
692 | 703 | |
|
693 | 704 | area.append( |
|
694 | 705 | $("<div/>") |
|
695 | 706 | .addClass("box-flex1 output_subarea raw_input") |
|
696 | 707 | .append( |
|
697 | 708 | $("<span/>") |
|
698 | 709 | .addClass("input_prompt") |
|
699 | 710 | .text(content.prompt) |
|
700 | 711 | ) |
|
701 | 712 | .append( |
|
702 | 713 | $("<input/>") |
|
703 | 714 | .addClass("raw_input") |
|
704 | 715 | .attr('type', 'text') |
|
705 | 716 | .attr("size", 47) |
|
706 | 717 | .keydown(function (event, ui) { |
|
707 | 718 | // make sure we submit on enter, |
|
708 | 719 | // and don't re-execute the *cell* on shift-enter |
|
709 | 720 | if (event.which === utils.keycodes.ENTER) { |
|
710 | 721 | that._submit_raw_input(); |
|
711 | 722 | return false; |
|
712 | 723 | } |
|
713 | 724 | }) |
|
714 | 725 | ) |
|
715 | 726 | ); |
|
716 | 727 | |
|
717 | 728 | this.element.append(area); |
|
718 | 729 | var raw_input = area.find('input.raw_input'); |
|
719 | 730 | // Register events that enable/disable the keyboard manager while raw |
|
720 | 731 | // input is focused. |
|
721 | 732 | IPython.keyboard_manager.register_events(raw_input); |
|
722 | 733 | // Note, the following line used to read raw_input.focus().focus(). |
|
723 | 734 | // This seemed to be needed otherwise only the cell would be focused. |
|
724 | 735 | // But with the modal UI, this seems to work fine with one call to focus(). |
|
725 | 736 | raw_input.focus(); |
|
726 | 737 | } |
|
727 | 738 | |
|
728 | 739 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
729 | 740 | var container = this.element.find("div.raw_input"); |
|
730 | 741 | var theprompt = container.find("span.input_prompt"); |
|
731 | 742 | var theinput = container.find("input.raw_input"); |
|
732 | 743 | var value = theinput.val(); |
|
733 | 744 | var content = { |
|
734 | 745 | output_type : 'stream', |
|
735 | 746 | name : 'stdout', |
|
736 | 747 | text : theprompt.text() + value + '\n' |
|
737 | 748 | } |
|
738 | 749 | // remove form container |
|
739 | 750 | container.parent().remove(); |
|
740 | 751 | // replace with plaintext version in stdout |
|
741 | 752 | this.append_output(content, false); |
|
742 | 753 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
743 | 754 | } |
|
744 | 755 | |
|
745 | 756 | |
|
746 | 757 | OutputArea.prototype.handle_clear_output = function (msg) { |
|
747 | 758 | this.clear_output(msg.content.wait); |
|
748 | 759 | }; |
|
749 | 760 | |
|
750 | 761 | |
|
751 | 762 | OutputArea.prototype.clear_output = function(wait) { |
|
752 | 763 | if (wait) { |
|
753 | 764 | |
|
754 | 765 | // If a clear is queued, clear before adding another to the queue. |
|
755 | 766 | if (this.clear_queued) { |
|
756 | 767 | this.clear_output(false); |
|
757 | 768 | }; |
|
758 | 769 | |
|
759 | 770 | this.clear_queued = true; |
|
760 | 771 | } else { |
|
761 | 772 | |
|
762 | 773 | // Fix the output div's height if the clear_output is waiting for |
|
763 | 774 | // new output (it is being used in an animation). |
|
764 | 775 | if (this.clear_queued) { |
|
765 | 776 | var height = this.element.height(); |
|
766 | 777 | this.element.height(height); |
|
767 | 778 | this.clear_queued = false; |
|
768 | 779 | } |
|
769 | 780 | |
|
770 | 781 | // clear all, no need for logic |
|
771 | 782 | this.element.html(""); |
|
772 | 783 | this.outputs = []; |
|
773 | 784 | this.unscroll_area(); |
|
774 | 785 | return; |
|
775 | 786 | }; |
|
776 | 787 | }; |
|
777 | 788 | |
|
778 | 789 | |
|
779 | 790 | // JSON serialization |
|
780 | 791 | |
|
781 | 792 | OutputArea.prototype.fromJSON = function (outputs) { |
|
782 | 793 | var len = outputs.length; |
|
783 | 794 | var data; |
|
784 | 795 | |
|
785 | 796 | // We don't want to display javascript on load, so remove it from the |
|
786 | 797 | // display order for the duration of this function call, but be sure to |
|
787 | 798 | // put it back in there so incoming messages that contain javascript |
|
788 | 799 | // representations get displayed |
|
789 | 800 | var js_index = OutputArea.display_order.indexOf('application/javascript'); |
|
790 | 801 | OutputArea.display_order.splice(js_index, 1); |
|
791 | 802 | |
|
792 | 803 | for (var i=0; i<len; i++) { |
|
793 | 804 | data = outputs[i]; |
|
794 | 805 | var msg_type = data.output_type; |
|
795 | 806 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
796 | 807 | // convert short keys to mime keys |
|
797 | 808 | // TODO: remove mapping of short keys when we update to nbformat 4 |
|
798 | 809 | data = this.rename_keys(data, OutputArea.mime_map_r); |
|
799 | 810 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); |
|
800 | 811 | } |
|
801 | 812 | |
|
802 | 813 | this.append_output(data); |
|
803 | 814 | } |
|
804 | 815 | |
|
805 | 816 | // reinsert javascript into display order, see note above |
|
806 | 817 | OutputArea.display_order.splice(js_index, 0, 'application/javascript'); |
|
807 | 818 | }; |
|
808 | 819 | |
|
809 | 820 | |
|
810 | 821 | OutputArea.prototype.toJSON = function () { |
|
811 | 822 | var outputs = []; |
|
812 | 823 | var len = this.outputs.length; |
|
813 | 824 | var data; |
|
814 | 825 | for (var i=0; i<len; i++) { |
|
815 | 826 | data = this.outputs[i]; |
|
816 | 827 | var msg_type = data.output_type; |
|
817 | 828 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
818 | 829 | // convert mime keys to short keys |
|
819 | 830 | data = this.rename_keys(data, OutputArea.mime_map); |
|
820 | 831 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); |
|
821 | 832 | } |
|
822 | 833 | outputs[i] = data; |
|
823 | 834 | } |
|
824 | 835 | return outputs; |
|
825 | 836 | }; |
|
826 | 837 | |
|
827 | 838 | |
|
828 | 839 | IPython.OutputArea = OutputArea; |
|
829 | 840 | |
|
830 | 841 | return IPython; |
|
831 | 842 | |
|
832 | 843 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now