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