Skip to content

Commit 19bde41

Browse files
authored
Map._to_png() without tempfile (#887)
Closes #882. Instead of storing the html in a temporary file, the html is kept as a string and fed to selenium that way. This solves the problem on Windows where a temporary file cannot be opened multiple times.
2 parents 067732c + 115c8ad commit 19bde41

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Bug Fixes
3131
- Unify `get_bounds` routine to avoid wrong responses
3232
- If Path option `fill_color` is present it will override `fill=False`
3333
- Fix disappearing layer control when using FastMarkerCluster (conengmo #866)
34+
- Host heatmap.js to circumvent adblockers (conengmo #886)
35+
- Fix permission error in Map._to_png() due to tempfile (conengmo #887)
3436

3537
0.5.0
3638
~~~~~

folium/folium.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,24 @@ def _to_png(self, delay=3):
299299
if self._png_image is None:
300300
import selenium.webdriver
301301

302-
with tempfile.NamedTemporaryFile(suffix='.html') as f:
303-
fname = f.name
304-
self.save(fname, close_file=False)
305-
driver = selenium.webdriver.PhantomJS(
306-
service_log_path=os.path.devnull
307-
)
308-
driver.get('file://{}'.format(fname))
309-
driver.maximize_window()
310-
# Ignore user map size.
311-
driver.execute_script("document.body.style.width = '100%';") # noqa
312-
# We should probably monitor if some element is present,
313-
# but this is OK for now.
314-
time.sleep(delay)
315-
png = driver.get_screenshot_as_png()
316-
driver.quit()
317-
self._png_image = png
302+
driver = selenium.webdriver.PhantomJS(
303+
service_log_path=os.path.devnull
304+
)
305+
driver.get('about:blank')
306+
html = self.get_root().render()
307+
html = html.replace('\'', '"').replace('"', '\\"')
308+
html = html.replace('\n', '')
309+
driver.execute_script('document.write(\"{}\")'.format(html))
310+
driver.maximize_window()
311+
# Ignore user map size.
312+
# todo: fix this
313+
# driver.execute_script("document.body.style.width = '100%';") # noqa
314+
# We should probably monitor if some element is present,
315+
# but this is OK for now.
316+
time.sleep(delay)
317+
png = driver.get_screenshot_as_png()
318+
driver.quit()
319+
self._png_image = png
318320
return self._png_image
319321

320322
def _repr_png_(self):

0 commit comments

Comments
 (0)