salekml commited on
Commit
de45c7c
Β·
verified Β·
1 Parent(s): 5fb50df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -1,25 +1,38 @@
1
  import gradio as gr
2
  import requests
3
 
4
- API_KEY = "PASTE_YOUR_API_KEY_HERE"
5
-
 
6
  def get_weather(city):
7
  if not city:
8
  return "<p style='color:red;'>Please enter a city name.</p>"
9
 
10
- url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
11
- response = requests.get(url)
12
- data = response.json()
 
 
 
13
 
14
- if response.status_code != 200:
15
  return "<p style='color:red;'>City not found!</p>"
16
 
17
- temp = data["main"]["temp"]
18
- humidity = data["main"]["humidity"]
19
- description = data["weather"][0]["description"].title()
20
- country = data["sys"]["country"]
 
 
 
 
 
 
 
 
 
21
 
22
- # Card style HTML
23
  card = f"""
24
  <div style="
25
  background: linear-gradient(135deg, #89f7fe, #66a6ff);
@@ -31,20 +44,28 @@ def get_weather(city):
31
  color: #333;
32
  box-shadow: 0 8px 16px rgba(0,0,0,0.3);
33
  ">
34
- <h2>🌍 {city}, {country}</h2>
35
- <h3>🌑 {temp}°C</h3>
36
  <p>πŸ’§ Humidity: {humidity}%</p>
37
  <p>☁ {description}</p>
38
  </div>
39
  """
40
  return card
41
 
 
 
 
42
  demo = gr.Interface(
43
  fn=get_weather,
44
- inputs=gr.Textbox(label="Enter City Name"),
45
  outputs=gr.HTML(), # HTML output for card style
46
- title="🌦 Weather App",
47
- description="Type a city name and get live weather data in a nice card style!"
48
  )
49
 
50
- demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
 
4
+ # =========================
5
+ # 🌦 Weather function (MetaWeather)
6
+ # =========================
7
  def get_weather(city):
8
  if not city:
9
  return "<p style='color:red;'>Please enter a city name.</p>"
10
 
11
+ # Step 1: Find WOEID for the city
12
+ search_url = f"https://www.metaweather.com/api/location/search/?query={city}"
13
+ try:
14
+ search_res = requests.get(search_url).json()
15
+ except:
16
+ return "<p style='color:red;'>Error connecting to MetaWeather API.</p>"
17
 
18
+ if not search_res:
19
  return "<p style='color:red;'>City not found!</p>"
20
 
21
+ woeid = search_res[0]["woeid"]
22
+
23
+ # Step 2: Get weather data using WOEID
24
+ weather_url = f"https://www.metaweather.com/api/location/{woeid}/"
25
+ try:
26
+ weather_data = requests.get(weather_url).json()
27
+ except:
28
+ return "<p style='color:red;'>Error fetching weather data.</p>"
29
+
30
+ today = weather_data["consolidated_weather"][0]
31
+ temp = today["the_temp"]
32
+ humidity = today["humidity"]
33
+ description = today["weather_state_name"]
34
 
35
+ # HTML Card style
36
  card = f"""
37
  <div style="
38
  background: linear-gradient(135deg, #89f7fe, #66a6ff);
 
44
  color: #333;
45
  box-shadow: 0 8px 16px rgba(0,0,0,0.3);
46
  ">
47
+ <h2>🌍 {city.title()}</h2>
48
+ <h3>🌑 {temp:.1f}°C</h3>
49
  <p>πŸ’§ Humidity: {humidity}%</p>
50
  <p>☁ {description}</p>
51
  </div>
52
  """
53
  return card
54
 
55
+ # =========================
56
+ # πŸš€ Gradio Interface
57
+ # =========================
58
  demo = gr.Interface(
59
  fn=get_weather,
60
+ inputs=gr.Textbox(label="Enter City Name", placeholder="e.g. Sylhet"),
61
  outputs=gr.HTML(), # HTML output for card style
62
+ title="🌦 Weather App (MetaWeather API)",
63
+ description="Type a city name and get live weather data in a nice card style! No API key required."
64
  )
65
 
66
+ # =========================
67
+ # Launch
68
+ # =========================
69
+ # Local test
70
+ demo.launch()
71
+ # Later for public link: demo.launch(share=True)