Skip to content

Commit 24e3162

Browse files
committed
Swap path to bytes
1 parent 2946c48 commit 24e3162

3 files changed

Lines changed: 40 additions & 62 deletions

File tree

app.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,57 +61,36 @@
6161
mag_limit = st.sidebar.slider("Magnitude Limit", 1, 8, 5)
6262

6363
# Session state to persist chart path
64-
if "chart_path" not in st.session_state:
65-
st.session_state.chart_path = None
66-
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-
)
64+
if "chart_bytes" not in st.session_state:
65+
st.session_state.chart_bytes = None
8766

8867
# ───────────────────────────────────────────────
8968
# Generate chart button
9069
# ───────────────────────────────────────────────
70+
# Generate chart
9171
if st.sidebar.button("Generate Chart"):
9272
tz = ZoneInfo("Europe/London")
9373
dt = datetime.combine(obs_date, obs_time).replace(tzinfo=tz)
9474

9575
with st.spinner("Generating chart, please wait..."):
96-
path = generate_plot(plot_type, dt, mag_limit)
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)
9780

98-
st.session_state.chart_path = path
81+
st.session_state.chart_bytes = img_bytes
9982
st.success("✅ Chart generated!")
10083

10184
# ───────────────────────────────────────────────
10285
# Display chart + download button
10386
# ───────────────────────────────────────────────
104-
if st.session_state.chart_path:
105-
st.image(
106-
st.session_state.chart_path,
107-
caption=f"{plot_type} chart from Glasgow",
108-
width="content",
87+
if st.session_state.chart_bytes:
88+
st.image(st.session_state.chart_bytes,
89+
caption=f"{plot_type} chart from Glasgow")
90+
91+
st.download_button(
92+
label="⬇️ Download chart",
93+
data=st.session_state.chart_bytes,
94+
file_name=f"glasgow_{plot_type.lower()}.png",
95+
mime="image/png",
10996
)
110-
111-
with open(st.session_state.chart_path, "rb") as f:
112-
st.download_button(
113-
label="⬇️ Download chart",
114-
data=f,
115-
file_name=f"glasgow_{plot_type.lower()}.png",
116-
mime="image/png",
117-
)

scripts/glasgow_horizon.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
2121
"""
22-
import os
22+
23+
import io
2324
from typing import Optional
2425

2526
from datetime import datetime
@@ -29,25 +30,22 @@
2930
from starplot.styles import PlotStyle, extensions
3031

3132
def make_horizon_plot(
32-
output_path: str = "images/glasgow_horizon.png",
3333
dt: Optional[datetime] = None,
34-
mag_limit: int = 5) -> str:
34+
mag_limit: int = 5,
35+
resolution: int = 1600) -> bytes:
3536
"""
36-
Generate a horizon star ploy
37+
Generate a horizon star plot.
3738
3839
Args:
39-
- output_path: The path the image will be exported to.
4040
- dt: The Datetime object representing the desired time for
4141
the star plot
4242
- mag_limit: The magitude limit for stars displayed on
4343
the star plot.
44+
- resolution: image resolution
4445
45-
Returns: The path for the created image.
46+
Returns: The image in bytes
4647
"""
4748

48-
# Ensure images folder exists
49-
os.makedirs("images", exist_ok=True)
50-
5149
# Default: In Glasgow timezone
5250
if dt is None:
5351
tz = ZoneInfo("Europe/London")
@@ -91,7 +89,9 @@ def make_horizon_plot(
9189
p.constellation_labels()
9290
p.horizon(labels={180: "SOUTH"})
9391

94-
# Export image
95-
p.export(output_path, padding=0.1)
92+
# Export to in-memory buffer
93+
buf = io.BytesIO()
94+
p.export(buf, format="png", resolution=resolution)
95+
buf.seek(0)
9696

97-
return output_path
97+
return buf.getvalue() # return PNG bytes

scripts/glasgow_zenith.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
"""
2222

23-
import os
23+
import io
2424
from typing import Optional
2525

2626
from datetime import datetime
@@ -30,25 +30,22 @@
3030

3131

3232
def make_zenith_plot(
33-
output_path="images/glasgow_zenith.png",
3433
dt: Optional[datetime] = None,
35-
mag_limit: int = 5) -> str:
34+
mag_limit: int = 5,
35+
resolution: int = 1600) -> bytes:
3636
"""
37-
Generate a zenith star ploy
37+
Generate a zenith star plot.
3838
3939
Args:
40-
- output_path: The path the image will be exported to.
4140
- dt: The Datetime object representing the desired time for
4241
the star plot
4342
- mag_limit: The magitude limit for stars displayed on
4443
the star plot.
44+
- resolution: image resolution
4545
46-
Returns: The path for the created image.
46+
Returns: The image in bytes
4747
"""
4848

49-
# Ensure images folder exists
50-
os.makedirs("images", exist_ok=True)
51-
5249
# Default: now in Glasgow timezone
5350
if dt is None:
5451
tz = ZoneInfo("Europe/London")
@@ -81,8 +78,10 @@ def make_zenith_plot(
8178
p.horizon()
8279
p.constellation_labels()
8380

84-
# Export image
85-
p.export(output_path, transparent=False)
81+
# Export to in-memory buffer
82+
buf = io.BytesIO()
83+
p.export(buf, format="png", resolution=resolution)
84+
buf.seek(0)
8685

87-
return output_path
86+
return buf.getvalue()
8887

0 commit comments

Comments
 (0)