Create weather.py
Browse files- weather.py +139 -0
weather.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
def get_weather_data(city, weather_api_key):
|
6 |
+
base_url = "https://api.openweathermap.org/data/2.5/weather?"
|
7 |
+
complete_url = base_url + "appid=" + weather_api_key + "&q=" + city
|
8 |
+
response = requests.get(complete_url)
|
9 |
+
return response.json()
|
10 |
+
|
11 |
+
def generate_weather_description(data):
|
12 |
+
try:
|
13 |
+
temperature = data['main']['temp'] - 273.15
|
14 |
+
description = data['weather'][0]['description']
|
15 |
+
wind_speed = data['wind']['speed']
|
16 |
+
breeze = breeze_status(wind_speed)
|
17 |
+
return f"The current weather in your city {description}, {breeze} with a temperature of {temperature:.1f}°C."
|
18 |
+
except Exception as e:
|
19 |
+
return str(e)
|
20 |
+
|
21 |
+
def breeze_status(wind_speed):
|
22 |
+
if wind_speed < 1:
|
23 |
+
return "Calm"
|
24 |
+
elif wind_speed < 5:
|
25 |
+
return "Light breeze"
|
26 |
+
elif wind_speed < 11:
|
27 |
+
return "Gentle breeze"
|
28 |
+
elif wind_speed < 19:
|
29 |
+
return "Moderate breeze"
|
30 |
+
elif wind_speed < 29:
|
31 |
+
return "Fresh breeze"
|
32 |
+
elif wind_speed < 39:
|
33 |
+
return "Strong breeze"
|
34 |
+
elif wind_speed < 50:
|
35 |
+
return "High wind"
|
36 |
+
elif wind_speed < 62:
|
37 |
+
return "Gale"
|
38 |
+
elif wind_speed < 75:
|
39 |
+
return "Strong gale"
|
40 |
+
else:
|
41 |
+
return "Storm"
|
42 |
+
|
43 |
+
def get_weekly_forecast(city, weather_api_key):
|
44 |
+
base_url = "https://api.openweathermap.org/data/2.5/forecast?"
|
45 |
+
complete_url = base_url + "appid=" + weather_api_key + "&q=" + city
|
46 |
+
response = requests.get(complete_url)
|
47 |
+
return response.json()
|
48 |
+
|
49 |
+
def display_weekly_forecast(data):
|
50 |
+
try:
|
51 |
+
st.write("Weekly Weather Forecast")
|
52 |
+
displayed_dates = set()
|
53 |
+
|
54 |
+
c1, c2, c3, c4, c5, c6, c7 = st.columns(7)
|
55 |
+
with c1:
|
56 |
+
st.metric("","Date")
|
57 |
+
with c2:
|
58 |
+
st.metric("","Temperature")
|
59 |
+
with c3:
|
60 |
+
st.metric("", "Weather Condition")
|
61 |
+
with c4:
|
62 |
+
st.metric("","Precipitation")
|
63 |
+
with c5:
|
64 |
+
st.metric("", "Wind Speed")
|
65 |
+
with c6:
|
66 |
+
st.metric("", "Humidity")
|
67 |
+
with c7:
|
68 |
+
st.metric("", "UV Index")
|
69 |
+
|
70 |
+
for day in data['list']:
|
71 |
+
date = datetime.datetime.fromtimestamp(day['dt']).strftime('%A, %B %d')
|
72 |
+
if date not in displayed_dates:
|
73 |
+
displayed_dates.add(date)
|
74 |
+
|
75 |
+
min_temp = day['main']['temp_min'] - 273.15
|
76 |
+
max_temp = day['main']['temp_max'] - 273.15
|
77 |
+
description = day['weather'][0]['description']
|
78 |
+
main_weather = day['weather'][0]['main']
|
79 |
+
precipitation = day.get('rain', 'None') if 'rain' in day else day.get('snow', 'None')
|
80 |
+
wind_speed = day['wind'].get('speed', 'Unknown') if 'wind' in day else 'Unknown'
|
81 |
+
humidity = day['main'].get('humidity', 'Unknown')
|
82 |
+
uv_index = day.get('uv_index', 'Unknown')
|
83 |
+
|
84 |
+
precipitation_label = "None" if precipitation == 'None' else f"{precipitation} mm"
|
85 |
+
wind_speed_label = "Unknown" if wind_speed == 'Unknown' else f"{wind_speed} m/s"
|
86 |
+
humidity_label = "Unknown" if humidity == 'Unknown' else f"{humidity}%"
|
87 |
+
uv_index_label = "Unknown" if uv_index == 'Unknown' else uv_index
|
88 |
+
|
89 |
+
with c1:
|
90 |
+
st.write(f"{date}")
|
91 |
+
with c2:
|
92 |
+
st.write(f"Min: {min_temp:.1f}°C\nMax: {max_temp:.1f}°C")
|
93 |
+
with c3:
|
94 |
+
st.write(f"{description.capitalize()}")
|
95 |
+
with c4:
|
96 |
+
st.write(f"{precipitation_label}")
|
97 |
+
with c5:
|
98 |
+
st.write(f"{wind_speed_label}")
|
99 |
+
with c6:
|
100 |
+
st.write(f"{humidity_label}")
|
101 |
+
with c7:
|
102 |
+
st.write(f"{uv_index_label}")
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
st.error("Error in displaying weekly forecast:" + str(e))
|
106 |
+
|
107 |
+
def main():
|
108 |
+
st.sidebar.title("Weather Forecasting with LLM")
|
109 |
+
city = st.sidebar.text_input("Enter city name")
|
110 |
+
|
111 |
+
weather_api_key = "6ec4c3a3f2fc502d15fdd18a1080a25b"
|
112 |
+
|
113 |
+
submit = st.sidebar.button("Get weather")
|
114 |
+
|
115 |
+
|
116 |
+
if submit:
|
117 |
+
st.title("Weather updates for " + city + " is:")
|
118 |
+
with st.spinner('Fetching weather data..'):
|
119 |
+
weather_data = get_weather_data(city , weather_api_key)
|
120 |
+
|
121 |
+
if weather_data.get("cod") != 404:
|
122 |
+
col1, col2 = st.columns(2)
|
123 |
+
with col1:
|
124 |
+
st.metric("Temperature ", f"{weather_data['main']['temp'] - 273.5:.2f}°c")
|
125 |
+
st.metric("Humidity", f"{weather_data['main']['humidity']}%")
|
126 |
+
with col2:
|
127 |
+
st.metric("Pressure ", f"{weather_data['main']['pressure']}hPa")
|
128 |
+
st.metric("Wind speed", f"{weather_data['wind']['speed']}m/s")
|
129 |
+
|
130 |
+
weather_description = generate_weather_description(weather_data)
|
131 |
+
st.write(weather_description)
|
132 |
+
else:
|
133 |
+
st.error("City not found or an error occurred!")
|
134 |
+
|
135 |
+
weekly_forecast_data = get_weekly_forecast(city, weather_api_key)
|
136 |
+
display_weekly_forecast(weekly_forecast_data)
|
137 |
+
|
138 |
+
if __name__ == "__main__":
|
139 |
+
main()
|