##// END OF EJS Templates
Support isolated metadata tag for any content...
Pablo de Oliveira -
Show More
@@ -363,30 +363,6 b' class Latex(DisplayObject):'
363
363
364 class SVG(DisplayObject):
364 class SVG(DisplayObject):
365
365
366 def __init__(self, data=None, url=None, filename=None, scoped=False):
367 """Create a SVG display object given raw data.
368
369 When this object is returned by an expression or passed to the
370 display function, it will result in the data being displayed
371 in the frontend. If the data is a URL, the data will first be
372 downloaded and then displayed.
373
374 Parameters
375 ----------
376 data : unicode, str or bytes
377 The Javascript source code or a URL to download it from.
378 url : unicode
379 A URL to download the data from.
380 filename : unicode
381 Path to a local file to load the data from.
382 scoped : bool
383 Should the SVG declarations be scoped.
384 """
385 if not isinstance(scoped, (bool)):
386 raise TypeError('expected bool, got: %r' % scoped)
387 self.scoped = scoped
388 super(SVG, self).__init__(data=data, url=url, filename=filename)
389
390 # wrap data in a property, which extracts the <svg> tag, discarding
366 # wrap data in a property, which extracts the <svg> tag, discarding
391 # document headers
367 # document headers
392 _data = None
368 _data = None
@@ -416,11 +392,7 b' class SVG(DisplayObject):'
416 self._data = svg
392 self._data = svg
417
393
418 def _repr_svg_(self):
394 def _repr_svg_(self):
419 if self.scoped:
395 return self.data
420 metadata = dict(scoped=True)
421 return self.data, metadata
422 else:
423 return self.data
424
396
425
397
426 class JSON(DisplayObject):
398 class JSON(DisplayObject):
@@ -327,7 +327,51 b' var IPython = (function (IPython) {'
327 }
327 }
328 return oa;
328 return oa;
329 };
329 };
330
330
331
332 OutputArea.prototype.create_output_subarea = function(md, classes) {
333 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
334 if (md['isolated']) {
335 // Create an iframe to isolate the subarea from the rest of the
336 // document
337 var iframe = $('<iframe/>');
338 iframe.attr('frameborder', 0);
339 iframe.attr('scrolling', 'auto');
340
341 // Once the iframe is loaded, the subarea is dynamically inserted
342 iframe.on('load', function() {
343 // Workaround needed by Firefox, to properly render svg inside
344 // iframes, see http://stackoverflow.com/questions/10177190/
345 // svg-dynamically-added-to-iframe-does-not-render-correctly
346 this.contentDocument.open();
347
348 // Insert the subarea into the iframe
349 // We must directly write the html. When using Jquery's append
350 // method, javascript is evaluated in the parent document and
351 // not in the iframe document.
352 this.contentDocument.write(subarea.html());
353
354 this.contentDocument.close();
355
356 var body = this.contentDocument.body;
357 // Adjust the iframe width and height
358 iframe.width(body.scrollWidth + 'px');
359 iframe.height(body.scrollHeight + 'px');
360 });
361
362 // Elements should be appended to the inner subarea and not to the
363 // iframe
364 iframe.append = function(that) {
365 subarea.append(that);
366 };
367
368 return iframe;
369 } else {
370 return subarea;
371 }
372 }
373
374
331 OutputArea.prototype._append_javascript_error = function (err, container) {
375 OutputArea.prototype._append_javascript_error = function (err, container) {
332 // display a message when a javascript error occurs in display output
376 // display a message when a javascript error occurs in display output
333 var msg = "Javascript error adding output!"
377 var msg = "Javascript error adding output!"
@@ -457,7 +501,7 b' var IPython = (function (IPython) {'
457
501
458
502
459 OutputArea.prototype.append_html = function (html, md, element) {
503 OutputArea.prototype.append_html = function (html, md, element) {
460 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
504 var toinsert = this.create_output_subarea(md, "output_html rendered_html");
461 toinsert.append(html);
505 toinsert.append(html);
462 element.append(toinsert);
506 element.append(toinsert);
463 };
507 };
@@ -465,7 +509,7 b' var IPython = (function (IPython) {'
465
509
466 OutputArea.prototype.append_javascript = function (js, md, container) {
510 OutputArea.prototype.append_javascript = function (js, md, container) {
467 // We just eval the JS code, element appears in the local scope.
511 // We just eval the JS code, element appears in the local scope.
468 var element = $("<div/>").addClass("output_subarea");
512 var element = this.create_output_subarea(md, "");
469 container.append(element);
513 container.append(element);
470 // Div for js shouldn't be drawn, as it will add empty height to the area.
514 // Div for js shouldn't be drawn, as it will add empty height to the area.
471 container.hide();
515 container.hide();
@@ -480,7 +524,7 b' var IPython = (function (IPython) {'
480
524
481
525
482 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
526 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
483 var toinsert = $("<div/>").addClass("output_subarea output_text");
527 var toinsert = this.create_output_subarea(md, "output_text");
484 // escape ANSI & HTML specials in plaintext:
528 // escape ANSI & HTML specials in plaintext:
485 data = utils.fixConsole(data);
529 data = utils.fixConsole(data);
486 data = utils.fixCarriageReturn(data);
530 data = utils.fixCarriageReturn(data);
@@ -494,40 +538,9 b' var IPython = (function (IPython) {'
494
538
495
539
496 OutputArea.prototype.append_svg = function (svg, md, element) {
540 OutputArea.prototype.append_svg = function (svg, md, element) {
497 var wrapper = $('<div/>').addClass('output_subarea output_svg');
541 var toinsert = this.create_output_subarea(md, "output_svg");
498 wrapper.append(svg);
542 toinsert.append(svg);
499 var svg_element = wrapper.children()[0];
543 element.append(toinsert);
500
501 if (md['scoped']) {
502 // To avoid style or use collisions between multiple svg figures,
503 // svg figures are wrapped inside an iframe.
504 var iframe = $('<iframe/>')
505 iframe.attr('frameborder', 0);
506 iframe.attr('scrolling', 'no');
507
508 // Once the iframe is loaded, the svg is dynamically inserted
509 iframe.on('load', function() {
510 // Set the iframe height and width to fit the svg
511 // (the +10 pixel offset handles the default body margins
512 // in Chrome)
513 iframe.width(svg_element.width.baseVal.value + 10);
514 iframe.height(svg_element.height.baseVal.value + 10);
515
516 // Workaround needed by Firefox, to properly render svg inside
517 // iframes, see http://stackoverflow.com/questions/10177190/
518 // svg-dynamically-added-to-iframe-does-not-render-correctly
519 iframe.contents()[0].open();
520 iframe.contents()[0].close();
521
522 // Insert the svg inside the iframe
523 var body = iframe.contents().find('body');
524 body.html(wrapper.html());
525 });
526
527 element.append(iframe);
528 } else {
529 element.append(wrapper);
530 }
531 };
544 };
532
545
533
546
@@ -559,7 +572,7 b' var IPython = (function (IPython) {'
559
572
560
573
561 OutputArea.prototype.append_png = function (png, md, element) {
574 OutputArea.prototype.append_png = function (png, md, element) {
562 var toinsert = $("<div/>").addClass("output_subarea output_png");
575 var toinsert = this.create_output_subarea(md, "output_png");
563 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
576 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
564 if (md['height']) {
577 if (md['height']) {
565 img.attr('height', md['height']);
578 img.attr('height', md['height']);
@@ -574,7 +587,7 b' var IPython = (function (IPython) {'
574
587
575
588
576 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
589 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
577 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
590 var toinsert = this.create_output_subarea(md, "output_jpeg");
578 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
591 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
579 if (md['height']) {
592 if (md['height']) {
580 img.attr('height', md['height']);
593 img.attr('height', md['height']);
@@ -591,7 +604,7 b' var IPython = (function (IPython) {'
591 OutputArea.prototype.append_latex = function (latex, md, element) {
604 OutputArea.prototype.append_latex = function (latex, md, element) {
592 // This method cannot do the typesetting because the latex first has to
605 // This method cannot do the typesetting because the latex first has to
593 // be on the page.
606 // be on the page.
594 var toinsert = $("<div/>").addClass("output_subarea output_latex");
607 var toinsert = this.create_output_subarea(md, "output_latex");
595 toinsert.append(latex);
608 toinsert.append(latex);
596 element.append(toinsert);
609 element.append(toinsert);
597 };
610 };
General Comments 0
You need to be logged in to leave comments. Login now