-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
46 lines (39 loc) · 1.51 KB
/
weather.py
File metadata and controls
46 lines (39 loc) · 1.51 KB
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
import streamlit as st
import requests
def get_weather(api_key, city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
weather_info = {
"city": data['name'],
"temperature": data['main']['temp'],
"description": data['weather'][0]['description'],
"humidity": data['main']['humidity'],
"wind_speed": data['wind']['speed']
}
return weather_info
else:
return None
def main():
st.title("Weather App")
api_key = "fe97c61377545f40e42b991521c6e97f"
city = st.text_input("Enter city name:")
if st.button("Get Weather"):
if city:
weather = get_weather(api_key, city)
if weather:
st.success(f"Weather in {weather['city']}:")
st.write(f"🌡️ Temperature: {weather['temperature']}°C")
st.write(f"🌤️ Description: {weather['description'].capitalize()}")
st.write(f"💧 Humidity: {weather['humidity']}%")
st.write(f"🌬️ Wind Speed: {weather['wind_speed']} m/s")
else:
st.error("Failed to fetch weather data.")
else:
st.warning("Please enter a city name.")
st.markdown("---")
if st.button("Visit Portfolio"):
st.markdown("[Rudransh Das](https://rudransh.rf.gd)")
if __name__ == "__main__":
main()