Skip to content

Commit 5887b9b

Browse files
authored
Fix special char encoding in notebook (#76)
* Fix special char encoding in notebook * Update docstring * Use percent-encoding instead of base64 * Add test case * gitignore geckodriver on windows
1 parent 7a6c5ab commit 5887b9b

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ examples/foo.html
5454
# documentation builds
5555
docs/_build
5656

57+
geckodriver.exe
5758
geckodriver.log
5859

5960
# Pycharm

branca/element.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import json
1111
import warnings
1212
from collections import OrderedDict
13+
import urllib.parse
1314
from urllib.request import urlopen
1415
from uuid import uuid4
1516

@@ -319,15 +320,20 @@ def render(self, **kwargs):
319320
return self._template.render(this=self, kwargs=kwargs)
320321

321322
def _repr_html_(self, **kwargs):
322-
"""Displays the Figure in a Jupyter notebook."""
323-
# Base64-encoded HTML is stored in a data-html attribute, which is used to populate
324-
# the iframe. This approach does not encounter the 2MB limit in Chrome for storing
325-
# the HTML in the src attribute with a data URI. The alternative of using a srcdoc
326-
# attribute is not supported in Microsoft Internet Explorer and Edge.
327-
html = base64.b64encode(self.render(**kwargs).encode('utf8')).decode('utf8')
323+
"""Displays the Figure in a Jupyter notebook.
324+
325+
Percent-encoded HTML is stored in data-html attribute, which is used to populate
326+
the iframe. This approach does not encounter the 2MB limit in Chrome for storing
327+
the HTML in the src attribute with a data URI. The alternative of using a srcdoc
328+
attribute is not supported in Microsoft Internet Explorer and Edge.
329+
330+
"""
331+
html = urllib.parse.quote(self.render(**kwargs))
328332
onload = (
329333
'this.contentDocument.open();'
330-
'this.contentDocument.write(atob(this.getAttribute(\'data-html\')));'
334+
'this.contentDocument.write('
335+
' decodeURIComponent(this.getAttribute(\'data-html\'))'
336+
');'
331337
'this.contentDocument.close();'
332338
)
333339

tests/test_iframe.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Folium Element Module class IFrame
55
----------------------
66
"""
7+
import os
78

89
import pytest
910
from selenium.webdriver import Firefox
@@ -33,3 +34,29 @@ def test_rendering_utf8_iframe():
3334
driver.get('data:text/html,' + iframe.render())
3435
driver.switch_to.frame(0)
3536
assert u'Cerrahpaşa Tıp Fakültesi' in driver.page_source
37+
38+
39+
@pytest.mark.headless
40+
def test_rendering_figure_notebook():
41+
"""Verify special characters are correctly rendered in Jupyter notebooks."""
42+
text = '5/7 %, Линейная улица, "\u00e9 Berdsk"'
43+
figure = elem.Figure()
44+
elem.Html(text).add_to(figure.html)
45+
html = figure._repr_html_()
46+
47+
filepath = 'temp_test_rendering_figure_notebook.html'
48+
filepath = os.path.abspath(filepath)
49+
with open(filepath, 'w') as f:
50+
f.write(html)
51+
52+
options = Options()
53+
options.add_argument('-headless')
54+
driver = Firefox(options=options)
55+
try:
56+
driver.get('file://' + filepath)
57+
driver.switch_to.frame(0)
58+
text_div = driver.find_element_by_css_selector('div')
59+
assert text_div.text == text
60+
finally:
61+
os.remove(filepath)
62+
driver.quit()

0 commit comments

Comments
 (0)