|
7 | 7 |
|
8 | 8 | from __future__ import (absolute_import, division, print_function)
|
9 | 9 |
|
10 |
| -import os |
11 | 10 | import time
|
12 | 11 | import warnings
|
13 | 12 |
|
| 13 | +from branca.colormap import StepColormap |
14 | 14 | from branca.element import CssLink, Element, Figure, JavascriptLink, MacroElement
|
15 | 15 | from branca.utilities import _parse_size
|
16 | 16 |
|
17 | 17 | from folium.map import FitBounds
|
18 | 18 | from folium.raster_layers import TileLayer
|
19 |
| -from folium.utilities import _validate_location |
| 19 | +from folium.utilities import _tmp_html, _validate_location |
20 | 20 |
|
21 | 21 | from jinja2 import Environment, PackageLoader, Template
|
22 | 22 |
|
| 23 | +import numpy as np |
| 24 | + |
| 25 | + |
23 | 26 | ENV = Environment(loader=PackageLoader('folium', 'templates'))
|
24 | 27 |
|
25 | 28 |
|
@@ -203,10 +206,10 @@ class Map(MacroElement):
|
203 | 206 | zoomControl: {{this.zoom_control.__str__().lower()}},
|
204 | 207 | });
|
205 | 208 | {% if this.control_scale %}L.control.scale().addTo({{this.get_name()}});{% endif %}
|
206 |
| - |
| 209 | +
|
207 | 210 | {% if this.objects_to_stay_in_front %}
|
208 | 211 | function objects_in_front() {
|
209 |
| - {% for obj in this.objects_to_stay_in_front %} |
| 212 | + {% for obj in this.objects_to_stay_in_front %} |
210 | 213 | {{ obj.get_name() }}.bringToFront();
|
211 | 214 | {% endfor %}
|
212 | 215 | };
|
@@ -291,35 +294,30 @@ def _repr_html_(self, **kwargs):
|
291 | 294 | def _to_png(self, delay=3):
|
292 | 295 | """Export the HTML to byte representation of a PNG image.
|
293 | 296 |
|
294 |
| - Uses Phantom JS to render the HTML and record a PNG. You may need to |
| 297 | + Uses selenium to render the HTML and record a PNG. You may need to |
295 | 298 | adjust the `delay` time keyword argument if maps render without data or tiles.
|
296 | 299 |
|
297 | 300 | Examples
|
298 | 301 | --------
|
299 | 302 | >>> map._to_png()
|
300 | 303 | >>> map._to_png(time=10) # Wait 10 seconds between render and snapshot.
|
301 |
| - """ |
302 | 304 |
|
| 305 | + """ |
303 | 306 | if self._png_image is None:
|
304 |
| - import selenium.webdriver |
| 307 | + from selenium import webdriver |
| 308 | + |
| 309 | + options = webdriver.firefox.options.Options() |
| 310 | + options.add_argument('--headless') |
| 311 | + driver = webdriver.Firefox(options=options) |
305 | 312 |
|
306 |
| - driver = selenium.webdriver.PhantomJS( |
307 |
| - service_log_path=os.path.devnull |
308 |
| - ) |
309 |
| - driver.get('about:blank') |
310 | 313 | html = self.get_root().render()
|
311 |
| - html = html.replace('\'', '"').replace('"', '\\"') |
312 |
| - html = html.replace('\n', '') |
313 |
| - driver.execute_script('document.write(\"{}\")'.format(html)) |
314 |
| - driver.maximize_window() |
315 |
| - # Ignore user map size. |
316 |
| - # todo: fix this |
317 |
| - # driver.execute_script("document.body.style.width = '100%';") # noqa |
318 |
| - # We should probably monitor if some element is present, |
319 |
| - # but this is OK for now. |
320 |
| - time.sleep(delay) |
321 |
| - png = driver.get_screenshot_as_png() |
322 |
| - driver.quit() |
| 314 | + with _tmp_html(html) as tmp: |
| 315 | + # We need the tempfile to avoid JS security issues. |
| 316 | + driver.get('file:///{path}'.format(path=tmp.name)) |
| 317 | + driver.maximize_window() |
| 318 | + time.sleep(delay) |
| 319 | + png = driver.get_screenshot_as_png() |
| 320 | + driver.quit() |
323 | 321 | self._png_image = png
|
324 | 322 | return self._png_image
|
325 | 323 |
|
|
0 commit comments