-
Notifications
You must be signed in to change notification settings - Fork 4
/
Weather_app.py
150 lines (127 loc) · 6.02 KB
/
Weather_app.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
import requests
import streamlit as st
import time
import math
import pickle as pk
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import streamlit as st
import plotly.express as px
import pandas as pd
def get_weather_details(city_name):
base_url = "https://api.openweathermap.org/data/2.5/weather"
params = {
'q': city_name,
'appid': "d73ec4f18aca81c32b1836a8ac2506e0"
}
try:
response = requests.get(base_url, params=params)
data = response.json()
# Check if the request was successful
if response.status_code == 200:
# Extract weather details
weather_details = {
'city': city_name,
'temperature': data['main']['temp'],
'description': data['weather'][0]['description'],
'humidity': data['main']['humidity'],
'wind_speed': data['wind']['speed']
}
return weather_details
else:
st.write("Error {}: {}".format(response.status_code, data['message']))
return None
except Exception as e:
st.write("An error occurred:", e)
return None
# -------------------------------------------------------------------------------------------Api---------------------------------------------------------
# Replace with your actual values
def run_weather_app():
st.title("City Weather Overview")
try:
global city_name
city_name = st.selectbox('Enter City',("Bagalkot", "Ballari", "Belagavi", "Bidar", "Chikkaballapur", "Chikkamagaluru", "Chitradurga", "Davanagere", "Dharwad", "Gadag", "Hassan", "Haveri", "Kalaburagi", "Kodagu", "Kolar", "Koppal", "Mandya", "Mysuru", "Raichur", "Ramanagara", "Shivamogga", "Tumakuru", "Udupi", "Uttara Kannada", "Vijayapura","Yadgir"),key="unique_key_2")
if city_name:
# api_key = "d73ec4f18aca81c32b1836a8ac2506e0"
# Get weather details
prediction = ''
if st.button('Show'):
progress = st.progress(0)
for i in range(100):
time.sleep(0.005)
progress.progress(i+1)
weather_data = get_weather_details(city_name)
if weather_data:
column1, column2 = st.columns(2)
column1.metric("City", weather_data['city'].capitalize())
column1.metric("Temperature",value=f"{ round(weather_data['temperature'] - 273.15, 2) } °C",delta= weather_data['description'])
# column1.metric("Description", weather_data['description'])
column1.metric("Humidity",value= f"{weather_data['humidity']} %")
column1.metric("Wind Speed",value= f"{math.ceil(weather_data['wind_speed']*3.6)} km/hr")
else:
st.error("Invalid City Name")
except Exception as e:
st.error("An error occurred:", e)
return None
###-------------------------------------------------------------------Current weather------------------------------------------------------------
forecast_model = pk.load(open('rain_forecast_model.pkl','rb'))
def run_forecast():
global forecast_data
num_months = st.number_input('Enter the number of months to forecast', min_value=1, max_value=48, value=1, step=1)
# st.write(num_months)
forecast_data = forecast_model.forecast(steps = 12+num_months)
st.title('Monthly Rainfall Overview')
df = pd.DataFrame({'Month': forecast_data.index.strftime('%Y-%m'), 'Values': np.round(forecast_data.values, 2)})
# Plotly bar chart with hover information
fig = px.bar(df, x='Month', y='Values', text='Values', color='Values',
labels={'Values': 'Precipitation'},
title='Monthly Forecast Data',
template='plotly',
color_continuous_scale='viridis')
# Set hover text to rounded values
fig.update_traces(texttemplate='%{text:.2f}', textposition='outside')
# Customize x-axis labels
# fig.update_xaxes(tickangle=45)
# Display the plot in Streamlit
st.plotly_chart(fig)
#-----------------------------------------------------------------------------------------------------------------------------
def india_precipitation():
st.title('India Monthly Precipitation')
data = pd.DataFrame({'Month': forecast_data.index.strftime('%Y-%m'), 'Precipitation': np.round(forecast_data.values, 2)})
fig = px.choropleth(data, locations=["India"] * len(data),
locationmode="country names",
color='Precipitation',
hover_name='Month',
animation_frame='Month', # Use 'Month' as the animation frame
range_color=[forecast_data.min(), forecast_data.max()], # Adjust the range
color_continuous_scale='Viridis'
)
st.plotly_chart(fig)
#--------------------------------------------------------------------------------------------------------------------------
def forecast_data_for_tab():
forecast_df = pd.DataFrame()
forecast_df['Months'] = forecast_data.index.strftime('%Y-%m')
forecast_df['Precipitation'] = forecast_data.values
st.table(forecast_df)
# Add a download button for CSV
csv_button = st.download_button(
label="Download Forecast Data as CSV",
data=forecast_df.to_csv(index=False).encode('utf-8'),
file_name='forecast_data.csv',
mime='text/csv'
)
#----------------------------------------------------------------------------------------------------------------
def weather_forecast_app():
# st.set_page_config(page_title="Weather App", page_icon=":cloud:")
if True:
run_weather_app()
if True:
tab1, tab2, tab3 = st.tabs(['Forecast Barplot', 'Forecast data', 'Map'])
with tab1:
run_forecast()
with tab2:
forecast_data_for_tab()
with tab3:
india_precipitation()