Skip to content

Commit 2e3470f

Browse files
committed
use firefox headless
1 parent 5706269 commit 2e3470f

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

.github/CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ First of all, thanks for your interest in contributing!
6060
4. Make change to your local copy of the folium repository
6161
5. Make sure the tests pass:
6262
* in the repository folder do `pip install -e .` (needed for notebook tests)
63-
* along with all the dependencies install `phantomjs` via `npm install -g phantomjs` or by downloading it from [here](http://phantomjs.org/download.html) and installing manually
6463
* run `python -m pytest tests`
6564
* resolve all errors
6665
6. Commit those changes

.travis.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ language: minimal
22

33
sudo: false
44

5+
env:
6+
- MOZ_HEADLESS=1
7+
8+
addons:
9+
firefox: latest
10+
511
env:
612
global:
713
- secure: "gRT413onDOvwgiHpNXRsiqo+ZZSjwwBpjZryQ9h6IqYw6cTN9YVivYF15uTMD//mZyFeHRz+F/7/0EG2z+UYIBKbgktiNMbie/KizwRBnCThGpcch1VeizkBkPluWSQXndXM6STkHvn0eZBZBBh0QdTm1qaI0babUmgZuWhrX38="
@@ -31,8 +37,13 @@ before_install:
3137
- conda update conda
3238
- conda config --remove channels defaults --force
3339
- conda config --add channels conda-forge --force
34-
- conda create --name TEST python=$PY phantomjs --file requirements.txt --file requirements-dev.txt
40+
- conda create --name TEST python=$PY --file requirements.txt --file requirements-dev.txt
3541
- source activate TEST
42+
# firefox headless driver
43+
- wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz -O geckodriver.tar.gz
44+
- mkdir geckodriver
45+
- tar -xzf geckodriver.tar.gz -C geckodriver
46+
- export PATH=$PATH:$PWD/geckodriver
3647

3748
- if [[ "$PY" == "2.7" ]]; then
3849
conda install mock ;

folium/folium.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from __future__ import (absolute_import, division, print_function)
99

10-
import os
1110
import time
1211
import warnings
1312

@@ -202,10 +201,10 @@ class Map(MacroElement):
202201
zoomControl: {{this.zoom_control.__str__().lower()}},
203202
});
204203
{% if this.control_scale %}L.control.scale().addTo({{this.get_name()}});{% endif %}
205-
204+
206205
{% if this.objects_to_stay_in_front %}
207206
function objects_in_front() {
208-
{% for obj in this.objects_to_stay_in_front %}
207+
{% for obj in this.objects_to_stay_in_front %}
209208
{{ obj.get_name() }}.bringToFront();
210209
{% endfor %}
211210
};
@@ -290,35 +289,30 @@ def _repr_html_(self, **kwargs):
290289
def _to_png(self, delay=3):
291290
"""Export the HTML to byte representation of a PNG image.
292291
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
294293
adjust the `delay` time keyword argument if maps render without data or tiles.
295294
296295
Examples
297296
--------
298297
>>> map._to_png()
299298
>>> map._to_png(time=10) # Wait 10 seconds between render and snapshot.
300-
"""
301299
300+
"""
302301
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)
304307

305-
driver = selenium.webdriver.PhantomJS(
306-
service_log_path=os.path.devnull
307-
)
308-
driver.get('about:blank')
309308
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 tmp:
310+
# We need the tempfile to avoid JS security issues.
311+
driver.get('file:///{path}'.format(path=tmp.name))
312+
driver.maximize_window()
313+
time.sleep(delay)
314+
png = driver.get_screenshot_as_png()
315+
driver.quit()
322316
self._png_image = png
323317
return self._png_image
324318

folium/utilities.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import os
88
import struct
99
import zlib
10+
from contextlib import contextmanager
11+
from tempfile import NamedTemporaryFile
1012

1113
import numpy as np
1214

@@ -385,3 +387,15 @@ def iter_points(x):
385387
return [x]
386388
else:
387389
return []
390+
391+
@contextmanager
392+
def _tmp_html(data):
393+
tmp = None
394+
try:
395+
tmp = NamedTemporaryFile(suffix='.html', prefix='folium_')
396+
tmp.write(data.encode('utf8'))
397+
tmp.flush()
398+
yield tmp
399+
finally:
400+
if tmp is not None:
401+
tmp.close()

0 commit comments

Comments
 (0)