This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskycast.py
176 lines (140 loc) · 6.86 KB
/
skycast.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
import streamlit as st
from get_geolocation.geolocation import GeolocationModel
from weather_forecast.forecast import WeatherForecast
import matplotlib.pyplot as plt
class SkyCast:
def __init__(self):
self.geolocation_model = GeolocationModel()
self.api_key = "YOUR_API_KEY"
self.base_url = "https://api.weatherbit.io/v2.0/current"
self.weather_option = "Today's Weather"
def title(self):
st.markdown(
'<div style="border: 1px solid #ccc; padding: 20px; border-radius: 10px;">'
'<h1 style="text-align: center; color: #0080FF;">SkyCast 🌤️</h1>'
'</div>',
unsafe_allow_html=True
)
def input(self):
st.sidebar.title("Weather Options")
self.weather_option = st.sidebar.radio(
"Choose Weather Option",
options=["Today's Weather", "Forecast Weather"],
key="weather_option"
)
if self.weather_option == "Today's Weather":
self.display_today_weather()
else:
self.display_forecast_weather()
def display_today_weather(self):
latitude, longitude = None, None
# Manually enter the city name
city_name = st.sidebar.text_input("Enter City Name", key="city_name")
if st.sidebar.button("Submit"):
latitude, longitude = self.geolocation_model.get_location_by_name(city_name)
else:
# If the submit button is not clicked, do not proceed further
return
st.markdown(f"<h2 style='text-align: center;'>Today's Weather</h2>", unsafe_allow_html=True)
if latitude is not None and longitude is not None:
# Make API request
response = self.get_weather_data(latitude, longitude)
if response is not None:
weather_data = response["data"][0]
# Weather details container
datetime_value = weather_data["datetime"]
datetime_value = datetime_value[:-3]
# Date and time
st.markdown(
f'<h3 style="text-align: center;">{weather_data["city_name"]}, {weather_data["country_code"]}: {datetime_value}</h3>',
unsafe_allow_html=True
)
# Additional weather information
col1, col2 = st.columns(2)
with col1:
st.markdown(f'Temperature: {weather_data["temp"]}°C')
st.markdown(f'Wind Speed: {weather_data["wind_spd"]} m/s')
st.markdown(f'Cloud Coverage: {weather_data["clouds"]}%')
st.markdown(f'Visibility: {weather_data["vis"]} km')
with col2:
st.markdown(f'Weather Description: {weather_data["weather"]["description"]}')
st.markdown(f'Wind Direction: {weather_data["wind_cdir_full"]}')
st.markdown(f'UV Index: {weather_data["uv"]}')
st.markdown(f'Dew Point: {weather_data["dewpt"]}°C')
else:
st.write("Location not found.")
def display_forecast_weather(self):
latitude, longitude = None, None
# Manually enter the city name
city_name = st.sidebar.text_input("Enter City Name", key="city_name")
if st.sidebar.button("Submit"):
latitude, longitude = self.geolocation_model.get_location_by_name(city_name)
else:
# If the submit button is not clicked, do not proceed further
return
st.markdown(f"<h2 style='text-align: center;'>Forecast Weather</h2>", unsafe_allow_html=True)
if latitude is not None and longitude is not None:
# Create an instance of the WeatherForecast class
weather = WeatherForecast(latitude, longitude, self.api_key)
# Retrieve the weather forecast data
forecast_df = weather.get_weather_forecast()
# Show forecast data
for i in range(7):
st.markdown(f'<h3 style="text-align: center;">{forecast_df["date"][i]}</h3>', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown(f'Temperature: {forecast_df["temperature"][i]}°C')
st.markdown(f'Wind Speed: {forecast_df["wind_speed"][i]} m/s')
st.markdown(f'Cloud Coverage: {forecast_df["cloud_coverage"][i]}%')
st.markdown(f'Visibility: {forecast_df["visibility"][i]} km')
with col2:
st.markdown(f'Weather Description: {forecast_df["weather_description"][i]}')
st.markdown(f'Wind Direction: {forecast_df["wind_direction"][i]}')
st.markdown(f'UV Index: {forecast_df["uv_index"][i]}')
st.markdown(f'Dew Point: {forecast_df["dew_point"][i]}°C')
temp = forecast_df["temperature"]
min_temp = forecast_df["min_temp"]
max_temp = forecast_df["max_temp"]
date = forecast_df["date"]
st.set_option('deprecation.showPyplotGlobalUse', False)
plt.figure(figsize=(10, 6)) # Set the figure size
# Plot the temperature line
plt.plot(date, temp, label='Temperature', marker='o', linestyle='-', color='blue')
# Plot the minimum temperature line
plt.plot(date, min_temp, label='Min Temperature', marker='o', linestyle='-', color='green')
# Plot the maximum temperature line
plt.plot(date, max_temp, label='Max Temperature', marker='o', linestyle='-', color='red')
# Set the labels and title
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Temperature Forecast')
# Add a legend
plt.legend()
# Display the plot
st.pyplot()
else:
st.write("Location not found.")
def get_weather_data(self, latitude, longitude):
params = {
"lat": latitude,
"lon": longitude,
"key": self.api_key,
"include": "minutely"
}
try:
response = requests.get(self.base_url, params=params)
if response.status_code == 200:
data = response.json()
return data
else:
st.write(f"Error: {response.status_code} - {response.text}")
return None
except requests.exceptions.RequestException as e:
st.write(f"Error: {e}")
return None
# Create an instance of the SkyCast class
weather_forecast = SkyCast()
# Display the title and input fields
weather_forecast.title()
weather_forecast.input()