Skip to content

Commit 0930054

Browse files
committed
do not render png on notebooks by default
1 parent 70986ce commit 0930054

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

folium/map.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import os
1414
import tempfile
15+
import time
1516

1617
import json
1718
from collections import OrderedDict
@@ -156,11 +157,13 @@ def __init__(self, location=None, width='100%', height='100%',
156157
min_lon=-180, max_lon=180, max_bounds=True,
157158
detect_retina=False, crs='EPSG3857', control_scale=False,
158159
prefer_canvas=False, no_touch=False, disable_3d=False,
159-
subdomains='abc'):
160+
subdomains='abc', png_enabled=False):
160161
super(LegacyMap, self).__init__()
161162
self._name = 'Map'
162163
self._env = ENV
164+
# Undocumented for now b/c this will be subject to a re-factor soon.
163165
self._png_image = None
166+
self.png_enabled = png_enabled
164167

165168
if not location:
166169
# If location is not passed we center and ignore zoom.
@@ -249,9 +252,8 @@ def _repr_html_(self, **kwargs):
249252
out = self._parent._repr_html_(**kwargs)
250253
return out
251254

252-
def _repr_png_(self):
253-
"""Displays the PNG Map in a Jupyter notebook."""
254-
# https://github.com/ipython/ipython/issues/7899#issuecomment-76541102
255+
def _to_png(self):
256+
"""Export the HTML to byte representation of a PNG image."""
255257
if self._png_image is None:
256258
import selenium.webdriver
257259

@@ -263,11 +265,21 @@ def _repr_png_(self):
263265
driver.maximize_window()
264266
# Ignore user map size.
265267
driver.execute_script("document.body.style.width = '100%';")
268+
# We should probably monitor if some element is present,
269+
# but this is OK for now.
270+
time.sleep(3)
266271
png = driver.get_screenshot_as_png()
267272
driver.quit()
268273
self._png_image = png
269274
return self._png_image
270275

276+
def _repr_png_(self):
277+
"""Displays the PNG Map in a Jupyter notebook."""
278+
# The notebook calls all _repr_*_ by default.
279+
# We don't want that here b/c this one is quite slow.
280+
if not self.png_enabled:
281+
return None
282+
return self._to_png()
271283

272284
def add_tile_layer(self, tiles='OpenStreetMap', name=None,
273285
API_key=None, max_zoom=18, min_zoom=1,

tests/test_repr.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616

1717
@pytest.fixture
18-
def make_map():
19-
m = folium.Map()
18+
def make_map(png_enabled=False):
19+
m = folium.Map(png_enabled=png_enabled)
2020
return m
2121

2222

@@ -37,12 +37,17 @@ def test_valid_html():
3737
assert parts[5] == '/div>'
3838

3939

40+
def test__repr_png_no_image():
41+
png = make_map(png_enabled=False)._repr_png_()
42+
assert png is None
43+
44+
4045
def test__repr_png_is_bytes():
41-
png = make_map()._repr_png_()
46+
png = make_map(png_enabled=True)._repr_png_()
4247
assert isinstance(png, bytes)
4348

4449

4550
def test_valid_png():
46-
png = make_map()._repr_png_()
51+
png = make_map(png_enabled=True)._repr_png_()
4752
img = PIL.Image.open(io.BytesIO(png))
4853
isinstance(img, PIL.PngImagePlugin.PngImageFile)

0 commit comments

Comments
 (0)