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 temp_html_filepath(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 test_geojson(driver): """Verify that loading data in GeoJson works well for different use cases. Prevent two regressions: - https://github.com/python-visualization/folium/pull/1190 - https://github.com/python-visualization/folium/pull/1289 """ data_url = 'https://cdn.jsdelivr.net/gh/python-visualization/folium@master/examples/data/search_bars_rome.json' m = folium.Map((41.9, 12.5), zoom_start=10, tiles='cartodbpositron') marker_cluster = folium.plugins.MarkerCluster(name='cluster').add_to(m) folium.GeoJson(data_url, embed=False).add_to(marker_cluster) folium.GeoJson(data_url, embed=False, show=False, name='geojson').add_to(m) folium.LayerControl(collapsed=False).add_to(m) html = m.get_root().render() with temp_html_filepath(html) as filepath: driver.get_file(filepath) assert driver.wait_until('.folium-map') driver.verify_js_logs() # Verify the marker cluster is shown, it's a yellow icon with '18' in it. icon = driver.wait_until('.leaflet-marker-icon.marker-cluster > div > span') assert icon.text == '18' # Verify the second GeoJson layer is not shown, because we used show=False. control_label = driver.wait_until( '.leaflet-control-layers-overlays > label:nth-of-type(2)' ) assert control_label.text == 'geojson' control_input = control_label.find_element_by_css_selector('input') assert control_input.get_attribute('checked') is None
def get_notebook_html(filepath_notebook, execute=True): """Store iframes from a notebook in html files, remove them when done.""" if execute: subprocess.run([ 'jupyter', 'nbconvert', '--to', 'notebook', '--execute', filepath_notebook, ]) filepath_notebook = filepath_notebook.replace('.ipynb', '.nbconvert.ipynb') html_exporter = nbconvert.HTMLExporter() html_exporter.template_file = 'basic' body, _ = html_exporter.from_filename(filepath_notebook) parser = IframeParser() parser.feed(body) iframes = parser.iframes for iframe in iframes: with temp_html_filepath(iframe) as filepath_html: yield filepath_html
def get_notebook_html(filepath_notebook): """Store iframes from a notebook in html files, remove them when done.""" # run the notebook to make sure the output is up-to-date subprocess.run([ 'jupyter', 'nbconvert', '--to', 'notebook', '--execute', filepath_notebook, ]) filepath_notebook = filepath_notebook.replace('.ipynb', '.nbconvert.ipynb') html_exporter = nbconvert.HTMLExporter() body, _ = html_exporter.from_filename(filepath_notebook) parser = IframeParser() parser.feed(body) iframes = parser.iframes for iframe in iframes: with temp_html_filepath(iframe) as filepath_html: yield filepath_html
def test_heat_map_with_weights(driver): """Verify that HeatMap uses weights in data correctly. This test will fail in non-headless mode because window size will be different. """ m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None) HeatMap( # make four dots with different weights: 1, 1, 1.5 and 2. data=[ (0, 0, 1.5), (0, 1, 1), (1, 0, 1), (1, 1, 2), ], radius=70, blur=50, ).add_to(m) html = m.get_root().render() with temp_html_filepath(html) as filepath: driver.get_file(filepath) assert driver.wait_until('.folium-map') driver.verify_js_logs() canvas = driver.wait_until('canvas.leaflet-heatmap-layer') assert canvas # get the canvas as a PNG base64 string canvas_base64 = driver.execute_script( "return arguments[0].toDataURL('image/png').substring(21);", canvas) screenshot = base64.b64decode(canvas_base64) path = os.path.dirname(__file__) with open(os.path.join(path, 'test_heat_map_selenium_screenshot.png'), 'rb') as f: screenshot_expected = f.read() if hash(screenshot) != hash(screenshot_expected): print(screenshot) assert False, 'screenshot is not as expected'