def _to_png(self, delay=3): """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. Examples -------- >>> m._to_png() >>> m._to_png(time=10) # Wait 10 seconds between render and snapshot. """ if self._png_image is None: from selenium import webdriver options = webdriver.firefox.options.Options() options.add_argument('--headless') driver = webdriver.Firefox(options=options) html = self.get_root().render() with _tmp_html(html) as fname: # We need the tempfile to avoid JS security issues. driver.get('file:///{path}'.format(path=fname)) driver.maximize_window() time.sleep(delay) png = driver.get_screenshot_as_png() driver.quit() self._png_image = png return self._png_image
def _save_map_to_png(m: folium.Map, filepath='mymap', delay=3): """ Saves a screenshot of a folium.Map object as a png. Similar to folium's existing m._to_png but saves to disk. WARNING - This is a private method because it required a non-essential dependency - you need to install geckodriver - see README for details. """ try: options = webdriver.firefox.options.Options() options.add_argument('--headless') driver = webdriver.Firefox(options=options) except: raise AssertionError( 'You need to install geckodriver - see README for details') html = m.get_root().render() with _tmp_html(html) as fname: # We need the tempfile to avoid JS security issues. driver.get(f'file:///{fname}') driver.maximize_window() time.sleep(delay) driver.save_screenshot(filepath) driver.quit()