-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
37 lines (30 loc) · 1.46 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
import streamlit as st
import plotly.express as px
from backend import get_data
# Add title, text input, slider, select box, and sub header
st.title("Weather Forecast for the Next Days")
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} days in {place}")
if place:
# Get the temperature/sky data
try:
filtered_data = get_data(place, days)
if option == "Temperature":
temperatures = [dict["main"]["temp"] for dict in filtered_data]
dates = [dict["dt_txt"] for dict in filtered_data]
# Create a temperature plot
figure = px.line(x=dates, y=temperatures, labels={"x": "Dates", "y": "Temperature (C)"})
st.plotly_chart(figure)
if option == "Sky":
images = {"Clear": "images/clear.png", "Clouds": "images/cloud.png",
"Rain": "images/rain.png", "Snow": "images/snow.png"}
sky_conditions = [dict["weather"][0]["main"] for dict in filtered_data]
dates = [dict["dt_txt"] for dict in filtered_data]
image_paths = [images[condition] for condition in sky_conditions]
st.image(image_paths, width=115, caption=dates)
except KeyError:
st.write("That place does not exist.")