Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2515,14 +2515,17 @@ def to_html(self, outfile: Optional[str] = None, **kwargs) -> str:
os.makedirs(out_dir)
self.save(outfile, **kwargs)
else:
outfile = os.path.abspath(common.random_string() + ".html")
self.save(outfile, **kwargs)
out_html = ""
with open(outfile) as f:
lines = f.readlines()
out_html = "".join(lines)
os.remove(outfile)
return out_html
outfile = common.temp_file_path(".html")
try:
self.save(outfile, **kwargs)
out_html = ""
with open(outfile) as f:
lines = f.readlines()
out_html = "".join(lines)
return out_html
finally:
if os.path.exists(outfile):
os.remove(outfile)

def to_streamlit(
self,
Expand Down
13 changes: 10 additions & 3 deletions leafmap/heremap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

from . import examples
from .basemaps import xyz_to_heremap
from .common import gdf_to_geojson, random_string, shp_to_geojson, vector_to_geojson
from .common import (
gdf_to_geojson,
shp_to_geojson,
temp_file_path,
vector_to_geojson,
)

try:
import here_map_widget
Expand Down Expand Up @@ -594,7 +599,7 @@ def to_html(
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
outfile = os.path.abspath(random_string() + ".html")
outfile = temp_file_path(".html")
save = False
Comment on lines 601 to 603
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When outfile is auto-generated via temp_file_path() (i.e., save=False), the temp file is only deleted on the happy path. If embed_minimal_html()/file read fails, the temp file can be left behind in the system temp dir. Consider wrapping file generation/read in try/finally so cleanup always happens when the outfile is implicitly created.

Copilot uses AI. Check for mistakes.

before_width = self.layout.width
Expand Down Expand Up @@ -629,11 +634,13 @@ def to_html(
with open(outfile) as f:
lines = f.readlines()
out_html = "".join(lines)
os.remove(outfile)
return out_html

except Exception as e:
raise Exception(e)
finally:
if not save and os.path.exists(outfile):
os.remove(outfile)

def to_streamlit(
self,
Expand Down
6 changes: 4 additions & 2 deletions leafmap/kepler.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def to_html(
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
outfile = os.path.abspath(common.random_string() + ".html")
outfile = common.temp_file_path(".html")
save = False

Comment on lines 459 to 462
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When outfile is auto-generated via common.temp_file_path() (i.e., save=False), the temp file is only deleted on the happy path. If save_to_html() or file read fails, the file can be left behind in the system temp dir. Consider using try/finally to guarantee cleanup when outfile is implicitly created.

Copilot uses AI. Check for mistakes.
output = widgets.Output()
Expand All @@ -469,11 +469,13 @@ def to_html(
with open(outfile) as f:
lines = f.readlines()
out_html = "".join(lines)
os.remove(outfile)
return out_html

except Exception as e:
raise Exception(e)
finally:
if not save and os.path.exists(outfile):
os.remove(outfile)

def to_streamlit(
self,
Expand Down
6 changes: 4 additions & 2 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2657,7 +2657,7 @@ def to_html(
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
outfile = os.path.abspath(common.random_string() + ".html")
outfile = common.temp_file_path(".html")
save = False
Comment on lines +2660 to 2661
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is meant to fix failures in read-only working directories, but the existing tests calling Map.to_html() don’t assert that it works when CWD is read-only. Please add a unit test that changes into a read-only temp directory (skip on platforms where chmod isn’t reliable) and verifies to_html() succeeds without an outfile.

Copilot uses AI. Check for mistakes.
Comment on lines 2659 to 2661
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When outfile is auto-generated via common.temp_file_path() (i.e., save=False), the temp file is only deleted on the happy path. If self.save()/read fails, the file can be left behind in the system temp dir. Consider wrapping the save/read in a try/finally to ensure the temp file is removed when it was created implicitly.

Copilot uses AI. Check for mistakes.

if add_layer_control and self.layer_control is None:
Expand Down Expand Up @@ -2697,11 +2697,13 @@ def to_html(
with open(outfile) as f:
lines = f.readlines()
out_html = "".join(lines)
os.remove(outfile)
return out_html

except Exception as e:
raise Exception(e)
finally:
if not save and os.path.exists(outfile):
os.remove(outfile)

def to_image(
self, outfile: Optional[str] = None, monitor: Optional[int] = 1
Expand Down
19 changes: 19 additions & 0 deletions tests/test_foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,25 @@ def test_to_html(self):
out_str = m.to_html()
assert "OpenStreetMap" in out_str

@unittest.skipUnless(os.name != "nt", "chmod not reliable on Windows")
def test_to_html_readonly_cwd(self):
"""Check to_html works when CWD is read-only (#1295)"""
import stat
import tempfile

tmpdir = tempfile.mkdtemp()
original_cwd = os.getcwd()
try:
os.chdir(tmpdir)
os.chmod(tmpdir, stat.S_IRUSR | stat.S_IXUSR)
m = leafmap.Map()
out_str = m.to_html()
assert "OpenStreetMap" in out_str
finally:
os.chmod(tmpdir, stat.S_IRWXU)
os.chdir(original_cwd)
os.rmdir(tmpdir)

def test_to_image(self):
"""Check map to image"""
with self.assertRaises(NotImplementedError):
Expand Down