Enable persistent download buttons for PNG and SVG files, and ensure placeholder image is displayed when no map is generated.

This commit is contained in:
Marcelo Prates
2025-06-29 16:55:21 -03:00
parent a5cb4ba9f3
commit d4cb905813
2 changed files with 42 additions and 32 deletions

74
app.py
View File

@@ -5,6 +5,7 @@ import sys
import os import os
import io import io
def download_svg(): def download_svg():
""" """
Creates additional map in SVG format Creates additional map in SVG format
@@ -74,8 +75,10 @@ with cols[0]:
page_size_col, dpi_col = st.columns(2) page_size_col, dpi_col = st.columns(2)
with page_size_col: with page_size_col:
page_size = st.selectbox( page_size = st.selectbox(
"Page Size", ["A4", "A5", "Square"], index = 0 "Page Size",
#, "A3", "A2", "A1", "Custom"], index=0 ["A4", "A5", "Square"],
index=0,
# , "A3", "A2", "A1", "Custom"], index=0
) )
with dpi_col: with dpi_col:
dpi = st.number_input("DPI", min_value=50, max_value=300, value=100, step=50) dpi = st.number_input("DPI", min_value=50, max_value=300, value=100, step=50)
@@ -139,7 +142,7 @@ with cols[1]:
use_container_width=True, use_container_width=True,
) )
if button: # or "last_image" in st.session_state: if button:
hillshade_params = ( hillshade_params = (
{ {
"azdeg": azdeg, "azdeg": azdeg,
@@ -175,36 +178,43 @@ with cols[1]:
with open(fig_path, "wb") as f: with open(fig_path, "wb") as f:
f.write(st.session_state.last_image.getbuffer()) f.write(st.session_state.last_image.getbuffer())
# Provide a download button for PNG file # Save SVG for persistent download
with open(fig_path, "rb") as file:
btn = st.download_button(
label="Download Map",
data=file,
file_name=f"{query}.png",
mime="image/png",
use_container_width=True,
)
# Provide a download button for SVG file
svg_path = download_svg() svg_path = download_svg()
with open(svg_path, "rb") as file: st.session_state.last_png_path = fig_path
btn = st.download_button( st.session_state.last_svg_path = svg_path
label="Download Map as SVG",
data=file,
file_name=f"{query}.svg",
mime="image/svg",
use_container_width=True,
)
# Always show download buttons (disabled if no image)
png_ready = "last_png_path" in st.session_state and os.path.exists(
st.session_state["last_png_path"]
)
svg_ready = "last_svg_path" in st.session_state and os.path.exists(
st.session_state["last_svg_path"]
)
btn_cols = st.columns(2)
with btn_cols[0]:
st.download_button(
label="Download PNG",
data=open(st.session_state["last_png_path"], "rb") if png_ready else b"",
file_name=f"{query}.png",
mime="image/png",
use_container_width=True,
disabled=not png_ready,
)
with btn_cols[1]:
st.download_button(
label="Download SVG",
data=open(st.session_state["last_svg_path"], "rb") if svg_ready else b"",
file_name=f"{query}.svg",
mime="image/svg",
use_container_width=True,
disabled=not svg_ready,
)
st.image(st.session_state.last_image, use_container_width=True) # Always show image (generated or placeholder)
if st.session_state.get("last_image"):
st.image(st.session_state.last_image, use_container_width=True)
else: else:
if st.session_state.last_image: st.image(
st.image(st.session_state.last_image, use_container_width=True) "https://github.com/marceloprates/prettymaps/blob/main/pictures/app_placeholder.png?raw=true",
else: use_container_width=True,
st.image( )
"https://github.com/marceloprates/prettymaps/blob/main/prints/app_placeholder.png?raw=true",
use_container_width=True,
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 913 KiB