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