Skip to content

Commit 2946c48

Browse files
committed
Make app load faster
1 parent 74fbefc commit 2946c48

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

.streamlit/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[server]
22
headless = true
33
enableCORS = false
4+
enableXsrfProtection = false
45
port = 10000 # Render will set PORT env var

app.py

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,99 @@
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
2121
"""
22+
2223
import os
2324
import pathlib
2425
import starplot
2526
import streamlit as st
2627
from datetime import datetime, date
2728
from zoneinfo import ZoneInfo
29+
30+
import starplot.data.constellations as condata
31+
import starplot.data.stars as stardata
32+
2833
from scripts.glasgow_horizon import make_horizon_plot
2934
from scripts.glasgow_zenith import make_zenith_plot
3035

31-
# Writable folder for Starplot / DuckDB extensions
36+
# ───────────────────────────────────────────────
37+
# Setup Starplot writable library (DuckDB extensions etc.)
38+
# ───────────────────────────────────────────────
3239
starplot_writable = pathlib.Path("starplot_library")
3340
os.makedirs(starplot_writable, exist_ok=True)
34-
# redirect internal library
3541
starplot.data.library_path = starplot_writable
3642

43+
# Preload datasets to avoid slow first-run
44+
try:
45+
condata.table()
46+
stardata.table()
47+
except Exception as e:
48+
print("Warning: preload failed:", e)
49+
50+
# ───────────────────────────────────────────────
51+
# Streamlit App
52+
# ───────────────────────────────────────────────
53+
st.set_page_config(page_title="Glasgow Starplot Viewer", page_icon="🌌")
3754

3855
st.title("🌌 Glasgow Starplot Viewer")
3956

40-
# Sidebar: Plot type
57+
# Sidebar: controls
4158
plot_type = st.sidebar.radio("Select Plot Type", ["Horizon", "Zenith"])
42-
43-
# Sidebar: Date and time
4459
obs_date = st.sidebar.date_input("Date", value=date.today())
4560
obs_time = st.sidebar.time_input("Time", value=datetime.now().time())
46-
47-
# Magnitude limit (stars)
4861
mag_limit = st.sidebar.slider("Magnitude Limit", 1, 8, 5)
4962

50-
# Ensure image remains displayed when download button pressed
63+
# Session state to persist chart path
5164
if "chart_path" not in st.session_state:
5265
st.session_state.chart_path = None
5366

54-
# Generate chart
67+
# ───────────────────────────────────────────────
68+
# Cached plot generator
69+
# ───────────────────────────────────────────────
70+
@st.cache_resource(show_spinner=False)
71+
def generate_plot(plot_type: str, dt: datetime, mag_limit: int) -> str:
72+
"""Generate and return path to starplot image."""
73+
if plot_type == "Horizon":
74+
return make_horizon_plot(
75+
output_path="images/glasgow_horizon.png",
76+
dt=dt,
77+
mag_limit=mag_limit,
78+
resolution=1600, # smaller for speed
79+
)
80+
else:
81+
return make_zenith_plot(
82+
output_path="images/glasgow_zenith.png",
83+
dt=dt,
84+
mag_limit=mag_limit,
85+
resolution=1600, # smaller for speed
86+
)
87+
88+
# ───────────────────────────────────────────────
89+
# Generate chart button
90+
# ───────────────────────────────────────────────
5591
if st.sidebar.button("Generate Chart"):
5692
tz = ZoneInfo("Europe/London")
5793
dt = datetime.combine(obs_date, obs_time).replace(tzinfo=tz)
5894

5995
with st.spinner("Generating chart, please wait..."):
60-
if plot_type == "Horizon":
61-
path = make_horizon_plot(output_path="images/glasgow_horizon.png", dt=dt, mag_limit=mag_limit)
62-
else:
63-
path = make_zenith_plot(output_path="images/glasgow_zenith.png", dt=dt, mag_limit=mag_limit)
96+
path = generate_plot(plot_type, dt, mag_limit)
6497

65-
st.session_state.chart_path = path # store path in session state
66-
st.success("Chart generated!")
98+
st.session_state.chart_path = path
99+
st.success("Chart generated!")
67100

68-
# Display the image if it exists in session state
101+
# ───────────────────────────────────────────────
102+
# Display chart + download button
103+
# ───────────────────────────────────────────────
69104
if st.session_state.chart_path:
70-
st.image(st.session_state.chart_path, caption=f"{plot_type} chart from Glasgow", width="content")
105+
st.image(
106+
st.session_state.chart_path,
107+
caption=f"{plot_type} chart from Glasgow",
108+
width="content",
109+
)
71110

72111
with open(st.session_state.chart_path, "rb") as f:
73112
st.download_button(
74-
label="Download chart",
113+
label="⬇️ Download chart",
75114
data=f,
76115
file_name=f"glasgow_{plot_type.lower()}.png",
77-
mime="image/png"
116+
mime="image/png",
78117
)

0 commit comments

Comments
 (0)