-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (36 loc) · 1.67 KB
/
main.py
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
import streamlit as st
import plotly.express as px
from backend import get_data
# Frontend
st.title("Weather Forecast App")
place = st.text_input("Place: ")
days = st.slider("Forecast Days", min_value=1, max_value=5,
help="Select the number of forecast days")
option = st.selectbox("Select data to view",
("Temperature", "Sky"))
st.subheader(f"{option} for the next {days} day(s) in {place}")
if place:
try:
# Get the temperature/sky data
filtered_data = get_data(place, days)
match option:
case "Temperature":
# Filter data again
temperatures = [dict["main"]["temp"] for dict in filtered_data]
dates = [dict["dt_txt"] for dict in filtered_data]
# Create temperature plot
figure = px.line(x=dates, y=temperatures, labels={"x": "Date", "y": "Temperature / °C"})
st.plotly_chart(figure)
case "Sky":
images = {"Clear": "images/clear.png",
"Clouds": "images/cloud.png",
"Rain": "images/rain.png",
"Snow": "images/snow.png"}
# Filter data again
sky_conditions = [dict["weather"][0]["main"] for dict in filtered_data]
# Create sky conditions plot with dates
dates = [dict["dt_txt"] for dict in filtered_data]
image_paths = [images[condition] for condition in sky_conditions]
st.image(image_paths, caption=dates, width=115)
except KeyError:
st.warning('This place does not exist!, maybe you have a typo!', icon="⚠️")