diff --git a/leafmap/foliumap.py b/leafmap/foliumap.py index b044d83987..0464ebbaaa 100644 --- a/leafmap/foliumap.py +++ b/leafmap/foliumap.py @@ -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, diff --git a/leafmap/heremap.py b/leafmap/heremap.py index 7e967355b3..6ff9bb655a 100644 --- a/leafmap/heremap.py +++ b/leafmap/heremap.py @@ -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 @@ -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 before_width = self.layout.width @@ -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, diff --git a/leafmap/kepler.py b/leafmap/kepler.py index a3232e95a4..9e36f7b236 100644 --- a/leafmap/kepler.py +++ b/leafmap/kepler.py @@ -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 output = widgets.Output() @@ -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, diff --git a/leafmap/leafmap.py b/leafmap/leafmap.py index 426b7f89b7..432e046170 100644 --- a/leafmap/leafmap.py +++ b/leafmap/leafmap.py @@ -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 if add_layer_control and self.layer_control is None: @@ -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 diff --git a/tests/test_foliumap.py b/tests/test_foliumap.py index 28e075ee1b..9633171598 100644 --- a/tests/test_foliumap.py +++ b/tests/test_foliumap.py @@ -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):