Skip to content

Commit e73b4c9

Browse files
committed
fix: folium.folium.Map._to_png()의 Firefox path related errors
fix: folium.folium.Map._to_png()의 Firefox path related errors [Problem] When the `folium.folium.Map._to_png()` method is used in an environment where the Firefox web browser is not installed, the following error is output. [Example] ``` WebDriverException: Message: 'geckodriver' executable needs to be in PATH. ``` [Solution] Added a Boolean argument called `chrome` to the `folium.folium.Map._to_png()` method. If this value is `False`, Firefox webdriver is used as before, and if this value is `True`, Chrome webdriver is used. Additionally, we used the `webdriver_manager` package, which automatically installs chromedriver considering the version of the Chrome web browser installed on the client. [Test] - Legacy error-generating logic ```python import io import folium from PIL import Image m = folium.Map(location = [37.53, 127.054], zoom_start = 14) img_data = m._to_png(5) img = Image.open(io.BytesIO(img_data)) ``` - Logic applied to the solution ```python import io import folium from PIL import Image m = folium.Map(location = [37.53, 127.054], zoom_start = 14) img_data = m._to_png(delay=5, chrome=True) img = Image.open(io.BytesIO(img_data)) ```
1 parent 551b242 commit e73b4c9

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

folium/folium.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def _repr_html_(self, **kwargs):
297297
out = self._parent._repr_html_(**kwargs)
298298
return out
299299

300-
def _to_png(self, delay=3):
300+
def _to_png(self, delay=3, chrome=False):
301301
"""Export the HTML to byte representation of a PNG image.
302302
303303
Uses selenium to render the HTML and record a PNG. You may need to
@@ -311,11 +311,15 @@ def _to_png(self, delay=3):
311311
"""
312312
if self._png_image is None:
313313
from selenium import webdriver
314-
315-
options = webdriver.firefox.options.Options()
316-
options.add_argument('--headless')
317-
driver = webdriver.Firefox(options=options)
318-
314+
if chrome is True:
315+
from webdriver_manager.chrome import ChromeDriverManager
316+
options = webdriver.ChromeOptions()
317+
options.add_argument('--headless')
318+
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
319+
else:
320+
options = webdriver.firefox.options.Options()
321+
options.add_argument('--headless')
322+
driver = webdriver.Firefox(options=options)
319323
html = self.get_root().render()
320324
with temp_html_filepath(html) as fname:
321325
# We need the tempfile to avoid JS security issues.

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ setuptools_scm
3030
selenium
3131
sphinx
3232
twine>=3.2.0
33+
webdriver-manager
3334
wheel
3435
vega_datasets
3536
vincent

0 commit comments

Comments
 (0)