Skip to content

Comments

Use system temp directory for temporary HTML files#1296

Merged
giswqs merged 2 commits intomasterfrom
fix-temp-file-path
Feb 19, 2026
Merged

Use system temp directory for temporary HTML files#1296
giswqs merged 2 commits intomasterfrom
fix-temp-file-path

Conversation

@giswqs
Copy link
Member

@giswqs giswqs commented Feb 19, 2026

Summary

  • Fixes Temporary files generated in app folder #1295
  • The to_html() methods in foliumap, leafmap, kepler, and heremap were creating temporary HTML files in the current working directory using random_string(). This fails in read-only environments (e.g., hardened Docker containers with read-only app directories).
  • Changed all four modules to use temp_file_path(".html") which writes to the system temp directory (/tmp) instead. This matches the existing pattern already used in deckgl.py.

Test plan

  • Verify to_html() without an outfile argument works correctly in foliumap, leafmap, kepler, and heremap
  • Verify to_streamlit() works correctly (it calls to_html() internally)
  • Verify behavior in a read-only working directory (e.g., Docker with --read-only flag)

Fixes #1295. The to_html() methods in foliumap, leafmap, kepler, and
heremap were creating temporary files in the current working directory
using random_string(). This fails in read-only environments like
hardened Docker containers. Changed to use temp_file_path() which
writes to the system temp directory instead.
Copilot AI review requested due to automatic review settings February 19, 2026 18:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates to_html() implementations to generate temporary HTML output in the system temp directory (instead of the current working directory), addressing failures in read-only runtime environments (e.g., hardened Docker containers) and aligning behavior across backends.

Changes:

  • Replaced os.path.abspath(random_string() + ".html") temp-file generation with temp_file_path(".html") in multiple backends.
  • Updated heremap.py imports to include temp_file_path for direct use.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
leafmap/leafmap.py Switches default to_html() temp outfile to common.temp_file_path(".html").
leafmap/kepler.py Switches default to_html() temp outfile to common.temp_file_path(".html").
leafmap/heremap.py Uses temp_file_path(".html") for default to_html() temp outfile and updates imports accordingly.
leafmap/foliumap.py Switches default to_html() temp outfile to common.temp_file_path(".html").
Comments suppressed due to low confidence (1)

leafmap/foliumap.py:2524

  • When outfile is auto-generated via common.temp_file_path(), the file is removed only on the success path. If self.save() or the subsequent read fails, the temp file can be left behind in the system temp dir. Consider using try/finally so the implicit temp file is always cleaned up.
            outfile = common.temp_file_path(".html")
            self.save(outfile, **kwargs)
            out_html = ""
            with open(outfile) as f:
                lines = f.readlines()
                out_html = "".join(lines)
            os.remove(outfile)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2660 to 2661
outfile = common.temp_file_path(".html")
save = False
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 2518 to 2519
outfile = common.temp_file_path(".html")
self.save(outfile, **kwargs)
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 test suite doesn’t currently cover calling to_html() with no outfile while CWD is read-only. Please add a unit test that runs foliumap.Map().to_html() from a read-only directory and asserts it still returns HTML.

Copilot uses AI. Check for mistakes.
from .common import gdf_to_geojson, random_string, shp_to_geojson, vector_to_geojson
from .common import (
gdf_to_geojson,
random_string,
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.

random_string is now imported from .common but no longer used after switching to temp_file_path(). Please remove the unused import to avoid flake8/pyflakes F401 unused-import failures.

Suggested change
random_string,

Copilot uses AI. Check for mistakes.
Comment on lines 2659 to 2661
else:
outfile = os.path.abspath(common.random_string() + ".html")
outfile = common.temp_file_path(".html")
save = False
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.
Comment on lines 459 to 462
else:
outfile = os.path.abspath(common.random_string() + ".html")
outfile = common.temp_file_path(".html")
save = False

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.
Comment on lines 602 to 604
else:
outfile = os.path.abspath(random_string() + ".html")
outfile = temp_file_path(".html")
save = False
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.
@github-actions
Copy link

github-actions bot commented Feb 19, 2026

@github-actions github-actions bot temporarily deployed to pull request February 19, 2026 18:30 Inactive
- Remove unused `random_string` import from heremap.py
- Add try/finally to ensure temp files are cleaned up on errors
  in foliumap, leafmap, kepler, and heremap
- Add unit test verifying to_html() works in a read-only directory
@github-actions github-actions bot temporarily deployed to pull request February 19, 2026 18:36 Inactive
@giswqs giswqs merged commit 84638b1 into master Feb 19, 2026
16 checks passed
@giswqs giswqs deleted the fix-temp-file-path branch February 19, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Temporary files generated in app folder

1 participant