0o7Hunk commited on
Commit
f739919
Β·
verified Β·
1 Parent(s): b87a54f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import geocoder
4
+
5
+ # πŸ‘‰ Put your API key here
6
+ API_KEY = "YOUR_API_KEY_HERE"
7
+
8
+ # -------- FUNCTIONS --------
9
+ def get_weather(city):
10
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
11
+ return requests.get(url).json()
12
+
13
+ def get_forecast(city):
14
+ url = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={API_KEY}&units=metric"
15
+ return requests.get(url).json()
16
+
17
+ def get_location():
18
+ g = geocoder.ip('me')
19
+ return g.city
20
+
21
+ # -------- UI --------
22
+ st.set_page_config(page_title="🌦 Weather App", layout="centered")
23
+
24
+ st.title("🌦 Smart Weather App")
25
+
26
+ # Auto location
27
+ if st.button("πŸ“ Use My Location"):
28
+ city = get_location()
29
+ st.success(f"Detected City: {city}")
30
+ else:
31
+ city = ""
32
+
33
+ # Manual input (voice removed because HF doesn't support mic well)
34
+ city = st.text_input("Enter City Name", city)
35
+
36
+ # Weather
37
+ if city:
38
+ weather = get_weather(city)
39
+
40
+ if weather.get("cod") != 200:
41
+ st.error("❌ City not found")
42
+ else:
43
+ st.subheader(f"πŸ“ Weather in {city}")
44
+
45
+ st.metric("🌑 Temperature", f"{weather['main']['temp']} °C")
46
+ st.metric("πŸ’§ Humidity", f"{weather['main']['humidity']} %")
47
+ st.write(f"πŸŒ₯ Condition: {weather['weather'][0]['description']}")
48
+
49
+ # Forecast
50
+ st.subheader("πŸ“… 5-Day Forecast")
51
+
52
+ forecast = get_forecast(city)
53
+
54
+ for i in range(0, 40, 8):
55
+ data = forecast['list'][i]
56
+ st.write(
57
+ f"πŸ“† {data['dt_txt']} | 🌑 {data['main']['temp']}Β°C | {data['weather'][0]['description']}"
58
+ )