1212
1313import os
1414import tempfile
15+ import time
1516
1617import json
1718from 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 ,
0 commit comments