-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
129 lines (101 loc) · 4.99 KB
/
app.py
File metadata and controls
129 lines (101 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
starplot-glasgow
Author: Beth Probert
Email: beth_probert@outlook.com
Copyright (C) 2025 Beth Probert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import pathlib
from datetime import datetime, date
from zoneinfo import ZoneInfo
import streamlit as st
import starplot
import starplot.data.constellations as condata
import starplot.data.stars as stardata
from scripts.glasgow_horizon import make_horizon_plot
from scripts.glasgow_zenith import make_zenith_plot
# ───────────────────────────────────────────────
# Setup Starplot writable library (DuckDB extensions etc.)
# ───────────────────────────────────────────────
starplot_writable = pathlib.Path("starplot_library")
os.makedirs(starplot_writable, exist_ok=True)
starplot.data.library_path = starplot_writable
# Preload datasets to avoid slow first-run
try:
condata.table()
stardata.table()
except Exception as e: # pylint: disable=broad-exception-caught
print("Warning: preload failed:", e)
# ───────────────────────────────────────────────
# Streamlit App
# ───────────────────────────────────────────────
st.set_page_config(page_title="Glasgow Starplot Viewer", page_icon="🌌")
st.title("🌌 Glasgow Starplot Viewer")
# Add a note about generation time
st.info(
"⚠️ Generating a chart can take up to **3 minutes** depending on your selections. "
"Please be patient while the image is being created."
)
# Sidebar: controls
plot_type = st.sidebar.radio("Select Plot Type", ["Horizon", "Zenith"])
obs_date = st.sidebar.date_input("Date", value=date.today())
obs_time = st.sidebar.time_input("Time", value=datetime.now().time())
mag_limit = st.sidebar.slider("Magnitude Limit", 1, 8, 5)
# Extra option: fast vs HD
quality = st.sidebar.radio("Quality", ["Fast (preview)", "HD (slower)"])
RESOLUTION = 1200 if quality == "Fast (preview)" else 2400
# Session state
if "chart_bytes" not in st.session_state:
st.session_state.chart_bytes = None
# ───────────────────────────────────────────────
# Cached chart generator
# ───────────────────────────────────────────────
@st.cache_data
def generate_chart(plt_type: str,
date_time: datetime,
mag_lim: int,
res: int) -> bytes:
"""
Generate chart and cache for performance
Args:
- plt_type: Either Horizon or Zenith
- date_time: Datetime for the star plot
- mag_lim: The maximum magnitude for stars in the plot
- res: Resolution of the image
Returns:
- The image in bytes
"""
if plt_type == "Horizon":
return make_horizon_plot(dt=date_time, mag_limit=mag_lim, resolution=res)
return make_zenith_plot(dt=date_time, mag_limit=mag_lim, resolution=res)
# ───────────────────────────────────────────────
# Generate chart button
# ───────────────────────────────────────────────
if st.sidebar.button("Generate Chart"):
tz = ZoneInfo("Europe/London")
dt = datetime.combine(obs_date, obs_time).replace(tzinfo=tz)
img_bytes = generate_chart(plot_type, dt, mag_limit, RESOLUTION)
st.session_state.chart_bytes = img_bytes
st.success("✅ Chart generated!")
# ───────────────────────────────────────────────
# Display chart + download button
# ───────────────────────────────────────────────
if st.session_state.chart_bytes:
st.image(st.session_state.chart_bytes,
caption=f"{plot_type} chart from Glasgow")
st.download_button(
label="⬇️ Download chart",
data=st.session_state.chart_bytes,
file_name=f"glasgow_{plot_type.lower()}.png",
mime="image/png",
)