Skip to content

Commit 5ae7d2f

Browse files
committed
PNG repr
1 parent 172df66 commit 5ae7d2f

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

folium/map.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
from __future__ import unicode_literals
1212

13+
import io
14+
import os
15+
import tempfile
16+
1317
import json
1418
from collections import OrderedDict
1519

@@ -157,6 +161,7 @@ def __init__(self, location=None, width='100%', height='100%',
157161
super(LegacyMap, self).__init__()
158162
self._name = 'Map'
159163
self._env = ENV
164+
self._png_image = None
160165

161166
if not location:
162167
# If location is not passed we center and ignore zoom.
@@ -236,8 +241,7 @@ def __init__(self, location=None, width='100%', height='100%',
236241
""") # noqa
237242

238243
def _repr_html_(self, **kwargs):
239-
"""Displays the Map in a Jupyter notebook.
240-
"""
244+
"""Displays the HTML Map in a Jupyter notebook."""
241245
if self._parent is None:
242246
self.add_to(Figure())
243247
out = self._parent._repr_html_(**kwargs)
@@ -246,6 +250,27 @@ def _repr_html_(self, **kwargs):
246250
out = self._parent._repr_html_(**kwargs)
247251
return out
248252

253+
def _repr_png_(self):
254+
"""Displays the PNG Map in a Jupyter notebook."""
255+
# https://github.com/ipython/ipython/issues/7899#issuecomment-76541102
256+
if self._png_image is None:
257+
import selenium.webdriver
258+
from PIL import Image
259+
260+
with tempfile.NamedTemporaryFile(suffix=".html") as f:
261+
fname = f.name
262+
self.save(fname)
263+
driver = selenium.webdriver.PhantomJS(service_log_path=os.path.devnull)
264+
driver.get('file:///{}'.format(fname))
265+
driver.maximize_window()
266+
# Ignore user map size.
267+
driver.execute_script("document.body.style.width = '100%';")
268+
png = driver.get_screenshot_as_png()
269+
driver.quit()
270+
self._png_image = png
271+
return self._png_image
272+
273+
249274
def add_tile_layer(self, tiles='OpenStreetMap', name=None,
250275
API_key=None, max_zoom=18, min_zoom=1,
251276
continuous_world=False, attr=None, active=False,

requirements-dev.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
pycodestyle
2-
pytest
3-
nbconvert
4-
jupyter_client
5-
ipykernel
6-
vincent
7-
geopandas
81
cartopy
9-
gpxpy
102
geographiclib
3+
geopandas
4+
gpxpy
5+
ipykernel
6+
jupyter_client
117
mplleaflet
8+
nbconvert
9+
pil
10+
pycodestyle
11+
pytest
12+
selenium
13+
vincent

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Jinja2
21
branca
3-
six
2+
jinja2
43
requests
4+
six

0 commit comments

Comments
 (0)