1010
1111from __future__ import unicode_literals
1212
13+ import os
14+ import tempfile
15+
1316import json
1417from collections import OrderedDict
1518
@@ -157,6 +160,7 @@ def __init__(self, location=None, width='100%', height='100%',
157160 super (LegacyMap , self ).__init__ ()
158161 self ._name = 'Map'
159162 self ._env = ENV
163+ self ._png_image = None
160164
161165 if not location :
162166 # If location is not passed we center and ignore zoom.
@@ -236,8 +240,7 @@ def __init__(self, location=None, width='100%', height='100%',
236240 """ ) # noqa
237241
238242 def _repr_html_ (self , ** kwargs ):
239- """Displays the Map in a Jupyter notebook.
240- """
243+ """Displays the HTML Map in a Jupyter notebook."""
241244 if self ._parent is None :
242245 self .add_to (Figure ())
243246 out = self ._parent ._repr_html_ (** kwargs )
@@ -246,6 +249,26 @@ def _repr_html_(self, **kwargs):
246249 out = self ._parent ._repr_html_ (** kwargs )
247250 return out
248251
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+ if self ._png_image is None :
256+ import selenium .webdriver
257+
258+ with tempfile .NamedTemporaryFile (suffix = ".html" ) as f :
259+ fname = f .name
260+ self .save (fname )
261+ driver = selenium .webdriver .PhantomJS (service_log_path = os .path .devnull )
262+ driver .get ('file://{}' .format (fname ))
263+ driver .maximize_window ()
264+ # Ignore user map size.
265+ driver .execute_script ("document.body.style.width = '100%';" )
266+ png = driver .get_screenshot_as_png ()
267+ driver .quit ()
268+ self ._png_image = png
269+ return self ._png_image
270+
271+
249272 def add_tile_layer (self , tiles = 'OpenStreetMap' , name = None ,
250273 API_key = None , max_zoom = 18 , min_zoom = 1 ,
251274 continuous_world = False , attr = None , active = False ,
0 commit comments