MCPcopy Index your code
hub / github.com/python-visualization/folium / _to_png

Method _to_png

folium/folium.py:361–406  ·  view source on GitHub ↗

Export the HTML to byte representation of a PNG image. Uses selenium to render the HTML and record a PNG. You may need to adjust the `delay` time keyword argument if maps render without data or tiles. Uses a headless Firefox webdriver by default, though you can provide your

(
        self, delay: int = 3, driver: Any = None, size: Optional[Sequence[int]] = None
    )

Source from the content-addressed store, hash-verified

359 return out
360
361 def _to_png(
362 self, delay: int = 3, driver: Any = None, size: Optional[Sequence[int]] = None
363 ) -> bytes:
364 """Export the HTML to byte representation of a PNG image.
365
366 Uses selenium to render the HTML and record a PNG. You may need to
367 adjust the `delay` time keyword argument if maps render without data or tiles.
368
369 Uses a headless Firefox webdriver by default, though you can provide your own.
370
371 Examples
372 --------
373 >>> m._to_png()
374 >>> m._to_png(time=10) # Wait 10 seconds between render and snapshot.
375
376 """
377
378 if self._png_image is None:
379 if driver is None:
380 from selenium import webdriver
381
382 options = webdriver.firefox.options.Options()
383 options.add_argument("--headless")
384 driver = webdriver.Firefox(options=options)
385
386 if size is None:
387 driver.fullscreen_window()
388 else:
389 window_size = driver.execute_script(
390 """
391 return [window.outerWidth - window.innerWidth + arguments[0],
392 window.outerHeight - window.innerHeight + arguments[1]];
393 """,
394 *size,
395 )
396 driver.set_window_size(*window_size)
397 html = self.get_root().render()
398 with temp_html_filepath(html) as fname:
399 # We need the tempfile to avoid JS security issues.
400 driver.get(f"file:///{fname}")
401 time.sleep(delay)
402 div = driver.find_element("class name", "folium-map")
403 png = div.screenshot_as_png
404 driver.quit()
405 self._png_image = png
406 return self._png_image
407
408 def _repr_png_(self) -> Optional[bytes]:
409 """Displays the PNG Map in a Jupyter notebook."""

Callers 2

_repr_png_Method · 0.95
test_screenshotFunction · 0.80

Calls 2

temp_html_filepathFunction · 0.90
renderMethod · 0.45

Tested by 1

test_screenshotFunction · 0.64