import gradio as gr
import requests
from datetime import datetime, timedelta
import json
def get_weather(city_name):
"""Fetch weather data for a given city using the provided API key"""
if not city_name.strip():
city_name = "Dubai" # Default city
try:
# Use the provided API key and URL structure
api_key = "openweather"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid=79007029562c2ab46617d801d8018750&units=metric"
response = requests.get(url)
data = response.json()
if data["cod"] == 200:
# Extract weather information
city = data["name"]
country = data["sys"]["country"]
temperature = int(data["main"]["temp"])
feels_like = int(data["main"]["feels_like"])
humidity = data["main"]["humidity"]
pressure = data["main"]["pressure"]
description = data["weather"][0]["description"].title()
wind_speed = data["wind"]["speed"]
visibility = data.get("visibility", 10000) // 1000 # Convert to km
# Mock some additional data for the UI
rain_chance = f"{humidity//3}%" # Mock rain chance based on humidity
return {
"city": city,
"country": country,
"temperature": temperature,
"feels_like": feels_like,
"description": description,
"humidity": humidity,
"pressure": pressure,
"wind_speed": wind_speed,
"visibility": visibility,
"rain_chance": rain_chance
}
else:
return None
except Exception as e:
return None
def format_weather_display(weather_data):
"""Format weather data for display"""
if not weather_data:
return "Sorry, I couldn't find weather information for that city."
# Create the main weather display
main_display = f"""
{weather_data['city']}
{weather_data['temperature']}°C
{weather_data['description']}
"""
# Create weather details grid
details_grid = f"""
WEATHER DETAILS
Chance of rain
{weather_data['rain_chance']}
💧
Pressure
{weather_data['pressure']} mb
📊
Wind
{weather_data['wind_speed']} km/h
💨
Feels like
{weather_data['feels_like']}°C
🌡️
Visibility
{weather_data['visibility']} km
👁️
Humidity
{weather_data['humidity']}%
💧
"""
return main_display + details_grid
def search_weather(city_name):
"""Search for weather and format the display"""
weather_data = get_weather(city_name)
return format_weather_display(weather_data)
# Custom CSS for the purple gradient background and card styling
custom_css = """
body, .gradio-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
min-height: 100vh;
}
.main-container {
background: white !important;
border-radius: 24px !important;
padding: 40px !important;
margin: 40px auto !important;
max-width: 900px !important;
box-shadow: 0 20px 40px rgba(0,0,0,0.1) !important;
}
.search-container {
display: flex !important;
align-items: center !important;
justify-content: space-between !important;
margin-bottom: 30px !important;
}
.app-title {
font-size: 20px !important;
font-weight: 600 !important;
color: #333 !important;
display: flex !important;
align-items: center !important;
}
.search-input {
flex: 1 !important;
margin: 0 20px !important;
border-radius: 12px !important;
border: 1px solid #e0e0e0 !important;
padding: 12px 16px !important;
font-size: 16px !important;
}
#weather_display {
border: none !important;
background: transparent !important;
}
"""
# Create the Gradio interface
with gr.Blocks(css=custom_css, title="Shafaq's Weather App") as app:
with gr.Column(elem_classes=["main-container"]):
# Header
with gr.Row(elem_classes=["search-container"]):
gr.HTML('🌤️ Shafaq\'s Weather App
')
city_input = gr.Textbox(
placeholder="Search for cities",
show_label=False,
elem_classes=["search-input"],
value="Dubai"
)
# Weather display
weather_display = gr.HTML(
value="",
elem_id="weather_display"
)
# Load Dubai weather on startup
app.load(
fn=search_weather,
inputs=gr.Textbox(value="Dubai", visible=False),
outputs=weather_display
)
# Search functionality
city_input.submit(
fn=search_weather,
inputs=city_input,
outputs=weather_display
)
# Launch the app
if __name__ == "__main__":
print("🌤️ Starting Shafaq's Weather App...")
print("🏙️ Default city: Dubai")
app.launch()