Skip to content

Commit 8247dce

Browse files
committed
Speed up performance with caching and low vs high res options
1 parent cf91eda commit 8247dce

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

app.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
# Streamlit App
5252
# ───────────────────────────────────────────────
5353
st.set_page_config(page_title="Glasgow Starplot Viewer", page_icon="🌌")
54-
5554
st.title("🌌 Glasgow Starplot Viewer")
5655

5756
# Sidebar: controls
@@ -60,27 +59,40 @@
6059
obs_time = st.sidebar.time_input("Time", value=datetime.now().time())
6160
mag_limit = st.sidebar.slider("Magnitude Limit", 1, 8, 5)
6261

63-
# Session state to persist chart path
62+
# Extra option: fast vs HD
63+
quality = st.sidebar.radio("Quality", ["Fast (preview)", "HD (slower)"])
64+
resolution = 1200 if quality == "Fast (preview)" else 2400
65+
66+
# Session state
6467
if "chart_bytes" not in st.session_state:
6568
st.session_state.chart_bytes = None
6669

70+
71+
# ───────────────────────────────────────────────
72+
# Cached chart generator
73+
# ───────────────────────────────────────────────
74+
@st.cache_data
75+
def generate_chart(plot_type, dt, mag_limit, resolution):
76+
if plot_type == "Horizon":
77+
return make_horizon_plot(dt=dt, mag_limit=mag_limit, resolution=resolution)
78+
else:
79+
return make_zenith_plot(dt=dt, mag_limit=mag_limit, resolution=resolution)
80+
81+
6782
# ───────────────────────────────────────────────
6883
# Generate chart button
6984
# ───────────────────────────────────────────────
70-
# Generate chart
7185
if st.sidebar.button("Generate Chart"):
7286
tz = ZoneInfo("Europe/London")
7387
dt = datetime.combine(obs_date, obs_time).replace(tzinfo=tz)
7488

7589
with st.spinner("Generating chart, please wait..."):
76-
if plot_type == "Horizon":
77-
img_bytes = make_horizon_plot(dt=dt, mag_limit=mag_limit)
78-
else:
79-
img_bytes = make_zenith_plot(dt=dt, mag_limit=mag_limit)
90+
img_bytes = generate_chart(plot_type, dt, mag_limit, resolution)
8091

8192
st.session_state.chart_bytes = img_bytes
8293
st.success("✅ Chart generated!")
8394

95+
8496
# ───────────────────────────────────────────────
8597
# Display chart + download button
8698
# ───────────────────────────────────────────────

scripts/glasgow_horizon.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
def make_horizon_plot(
3333
dt: Optional[datetime] = None,
34-
mag_limit: int = 5) -> bytes:
34+
mag_limit: int = 5,
35+
resolution: int = 1600) -> bytes:
3536
"""
3637
Generate a horizon star plot.
3738
@@ -40,6 +41,7 @@ def make_horizon_plot(
4041
the star plot
4142
- mag_limit: The magitude limit for stars displayed on
4243
the star plot.
44+
- resolution: image resolution
4345
4446
Returns: The image in bytes
4547
"""
@@ -69,7 +71,7 @@ def make_horizon_plot(
6971
azimuth=(135, 225),
7072
observer=observer,
7173
style=style,
72-
resolution=1600, # reduce size for faster rendering
74+
resolution=resolution,
7375
scale=0.9,
7476
)
7577

scripts/glasgow_zenith.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
def make_zenith_plot(
3333
dt: Optional[datetime] = None,
34-
mag_limit: int = 5) -> bytes:
34+
mag_limit: int = 5,
35+
resolution: int = 1600) -> bytes:
3536
"""
3637
Generate a zenith star plot.
3738
@@ -40,6 +41,7 @@ def make_zenith_plot(
4041
the star plot
4142
- mag_limit: The magitude limit for stars displayed on
4243
the star plot.
44+
- resolution: image resolution
4345
4446
Returns: The image in bytes
4547
"""
@@ -65,7 +67,7 @@ def make_zenith_plot(
6567
# Create Zenith plot
6668
p = ZenithPlot(
6769
observer=observer,
68-
resolution=1600, # smaller for faster rendering
70+
resolution=resolution,
6971
scale=0.9,
7072
style=style
7173
)

0 commit comments

Comments
 (0)