10
10
11
11
from __future__ import unicode_literals
12
12
13
+ import io
14
+ import os
15
+ import tempfile
16
+
13
17
import json
14
18
from collections import OrderedDict
15
19
@@ -157,6 +161,7 @@ def __init__(self, location=None, width='100%', height='100%',
157
161
super (LegacyMap , self ).__init__ ()
158
162
self ._name = 'Map'
159
163
self ._env = ENV
164
+ self ._png_image = None
160
165
161
166
if not location :
162
167
# If location is not passed we center and ignore zoom.
@@ -236,8 +241,7 @@ def __init__(self, location=None, width='100%', height='100%',
236
241
""" ) # noqa
237
242
238
243
def _repr_html_ (self , ** kwargs ):
239
- """Displays the Map in a Jupyter notebook.
240
- """
244
+ """Displays the HTML Map in a Jupyter notebook."""
241
245
if self ._parent is None :
242
246
self .add_to (Figure ())
243
247
out = self ._parent ._repr_html_ (** kwargs )
@@ -246,6 +250,27 @@ def _repr_html_(self, **kwargs):
246
250
out = self ._parent ._repr_html_ (** kwargs )
247
251
return out
248
252
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
+
249
274
def add_tile_layer (self , tiles = 'OpenStreetMap' , name = None ,
250
275
API_key = None , max_zoom = 18 , min_zoom = 1 ,
251
276
continuous_world = False , attr = None , active = False ,
0 commit comments