Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
def get_weather(city):
|
| 7 |
if not city:
|
| 8 |
return "<p style='color:red;'>Please enter a city name.</p>"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
if
|
| 15 |
return "<p style='color:red;'>City not found!</p>"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Card style
|
| 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}
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|