Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import pandas as pd
|
|
| 5 |
# OpenWeather API details
|
| 6 |
BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
|
| 7 |
API_KEY = "2682f6801720ebeb43c93ad55c826c5c"
|
|
|
|
| 8 |
|
| 9 |
def get_weather(city):
|
| 10 |
url = f"{BASE_URL}q={city}&appid={API_KEY}&units=metric"
|
|
@@ -17,22 +18,21 @@ def get_weather(city):
|
|
| 17 |
|
| 18 |
# Organizing data by date
|
| 19 |
for entry in response["list"]:
|
| 20 |
-
date = entry["dt_txt"].split(" ")[0]
|
| 21 |
-
time = entry["dt_txt"].split(" ")[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if date not in weather_info:
|
| 23 |
weather_info[date] = []
|
| 24 |
|
| 25 |
-
weather_info[date].append([
|
| 26 |
-
time,
|
| 27 |
-
f"{entry['main']['temp']}°C",
|
| 28 |
-
f"{entry['main']['humidity']}%",
|
| 29 |
-
f"{entry['main']['pressure']} hPa",
|
| 30 |
-
f"{entry['wind']['speed']} m/s",
|
| 31 |
-
f"{entry['rain']['3h']} mm" if "rain" in entry and "3h" in entry["rain"] else "0 mm",
|
| 32 |
-
f"{entry['clouds']['all']}%"
|
| 33 |
-
])
|
| 34 |
|
| 35 |
-
# Formatting output as tables inside boxes
|
| 36 |
markdown_output = """
|
| 37 |
<h2 style="text-align:center; color: #fff; background: linear-gradient(to right, #667eea, #764ba2); padding: 10px; border-radius: 10px;">
|
| 38 |
📅 5-Day Weather Forecast
|
|
@@ -40,19 +40,22 @@ def get_weather(city):
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
for date, details in weather_info.items():
|
| 43 |
-
df = pd.DataFrame(details, columns=["Time", "Temperature", "Humidity", "Pressure", "Wind Speed", "Rain", "Cloud Cover"])
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
markdown_output += f"""
|
| 47 |
<div class='weather-box'>
|
| 48 |
<h3 class="date-header">📆 {date}</h3>
|
| 49 |
-
{
|
| 50 |
</div>
|
| 51 |
"""
|
| 52 |
|
| 53 |
return markdown_output
|
| 54 |
|
| 55 |
-
# **
|
| 56 |
custom_css = """
|
| 57 |
body {
|
| 58 |
background: #0f172a;
|
|
@@ -101,6 +104,10 @@ h1, h2, h3 {
|
|
| 101 |
.styled-table tbody tr:hover {
|
| 102 |
background: rgba(255, 255, 255, 0.1);
|
| 103 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
"""
|
| 105 |
|
| 106 |
# **Gradio Interface**
|
|
|
|
| 5 |
# OpenWeather API details
|
| 6 |
BASE_URL = "http://api.openweathermap.org/data/2.5/forecast?"
|
| 7 |
API_KEY = "2682f6801720ebeb43c93ad55c826c5c"
|
| 8 |
+
ICON_URL = "http://openweathermap.org/img/wn/"
|
| 9 |
|
| 10 |
def get_weather(city):
|
| 11 |
url = f"{BASE_URL}q={city}&appid={API_KEY}&units=metric"
|
|
|
|
| 18 |
|
| 19 |
# Organizing data by date
|
| 20 |
for entry in response["list"]:
|
| 21 |
+
date = entry["dt_txt"].split(" ")[0]
|
| 22 |
+
time = entry["dt_txt"].split(" ")[1]
|
| 23 |
+
temp = f"{entry['main']['temp']}°C"
|
| 24 |
+
humidity = f"{entry['main']['humidity']}%"
|
| 25 |
+
pressure = f"{entry['main']['pressure']} hPa"
|
| 26 |
+
wind_speed = f"{entry['wind']['speed']} m/s"
|
| 27 |
+
rain = f"{entry['rain']['3h']} mm" if "rain" in entry and "3h" in entry["rain"] else "0 mm"
|
| 28 |
+
cloud_cover = f"{entry['clouds']['all']}%"
|
| 29 |
+
icon = f"{ICON_URL}{entry['weather'][0]['icon']}@2x.png" # Fetch weather icon
|
| 30 |
+
|
| 31 |
if date not in weather_info:
|
| 32 |
weather_info[date] = []
|
| 33 |
|
| 34 |
+
weather_info[date].append([time, temp, humidity, pressure, wind_speed, rain, cloud_cover, icon])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
markdown_output = """
|
| 37 |
<h2 style="text-align:center; color: #fff; background: linear-gradient(to right, #667eea, #764ba2); padding: 10px; border-radius: 10px;">
|
| 38 |
📅 5-Day Weather Forecast
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
for date, details in weather_info.items():
|
| 43 |
+
df = pd.DataFrame(details, columns=["Time", "Temperature", "Humidity", "Pressure", "Wind Speed", "Rain", "Cloud Cover", "Icon"])
|
| 44 |
+
df_html = df.to_html(index=False, escape=False) # Disable escaping for images
|
| 45 |
+
|
| 46 |
+
# Convert "Icon" column to actual images
|
| 47 |
+
df_html = df_html.replace("<img", "<img").replace(">", ">") # Fix HTML escaping for images
|
| 48 |
|
| 49 |
markdown_output += f"""
|
| 50 |
<div class='weather-box'>
|
| 51 |
<h3 class="date-header">📆 {date}</h3>
|
| 52 |
+
{df_html}
|
| 53 |
</div>
|
| 54 |
"""
|
| 55 |
|
| 56 |
return markdown_output
|
| 57 |
|
| 58 |
+
# **CSS for styling**
|
| 59 |
custom_css = """
|
| 60 |
body {
|
| 61 |
background: #0f172a;
|
|
|
|
| 104 |
.styled-table tbody tr:hover {
|
| 105 |
background: rgba(255, 255, 255, 0.1);
|
| 106 |
}
|
| 107 |
+
.styled-table img {
|
| 108 |
+
width: 50px; /* Weather icons size */
|
| 109 |
+
height: 50px;
|
| 110 |
+
}
|
| 111 |
"""
|
| 112 |
|
| 113 |
# **Gradio Interface**
|