Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a srcdoc attribute on iframes #96

Merged
merged 1 commit into from
Jun 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions branca/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"""

import base64
from html import escape
import json
import warnings
from collections import OrderedDict
import urllib.parse
from urllib.request import urlopen
from uuid import uuid4

Expand Down Expand Up @@ -320,43 +320,26 @@ def render(self, **kwargs):
return self._template.render(this=self, kwargs=kwargs)

def _repr_html_(self, **kwargs):
"""Displays the Figure in a Jupyter notebook.

Percent-encoded HTML is stored in data-html attribute, which is used to populate
the iframe. This approach does not encounter the 2MB limit in Chrome for storing
the HTML in the src attribute with a data URI. The alternative of using a srcdoc
attribute is not supported in Microsoft Internet Explorer and Edge.

"""
html = urllib.parse.quote(self.render(**kwargs))
onload = (
'this.contentDocument.open();'
'this.contentDocument.write('
' decodeURIComponent(this.getAttribute(\'data-html\'))'
');'
'this.contentDocument.close();'
)

"""Displays the Figure in a Jupyter notebook."""
html = escape(self.render(**kwargs))
dstein64 marked this conversation as resolved.
Show resolved Hide resolved
if self.height is None:
iframe = (
'<div style="width:{width};">'
'<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa
'<span style="color:#565656">Make this Notebook Trusted to load map: File -> Trust Notebook</span>' # noqa
dstein64 marked this conversation as resolved.
Show resolved Hide resolved
'<iframe src="about:blank" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'<iframe srcdoc="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'border:none !important;" '
'data-html={html} onload="{onload}" '
'allowfullscreen webkitallowfullscreen mozallowfullscreen>'
'</iframe>'
'</div></div>'
).format(html=html, onload=onload, width=self.width, ratio=self.ratio)
).format(html=html, width=self.width, ratio=self.ratio)
else:
iframe = (
'<iframe src="about:blank" width="{width}" height="{height}"'
'<iframe srcdoc="{html}" width="{width}" height="{height}"'
'style="border:none !important;" '
'data-html={html} onload="{onload}" '
'"allowfullscreen" "webkitallowfullscreen" "mozallowfullscreen">'
'</iframe>'
).format(html=html, onload=onload, width=self.width, height=self.height)
).format(html=html, width=self.width, height=self.height)
return iframe

def add_subplot(self, x, y, n, margin=0.05):
Expand Down