Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| # 1. Helper function to decode WMO Weather Codes into human-readable text | |
| def get_weather_description(code): | |
| # WMO Weather interpretation codes (WW) | |
| codes = { | |
| 0: ("βοΈ", "Clear sky"), | |
| 1: ("π€οΈ", "Mainly clear"), | |
| 2: ("β ", "Partly cloudy"), | |
| 3: ("βοΈ", "Overcast"), | |
| 45: ("π«οΈ", "Fog"), | |
| 48: ("π«οΈ", "Depositing rime fog"), | |
| 51: ("π¦οΈ", "Light Drizzle"), | |
| 53: ("π¦οΈ", "Moderate Drizzle"), | |
| 55: ("π§οΈ", "Dense Drizzle"), | |
| 61: ("β", "Slight Rain"), | |
| 63: ("β", "Moderate Rain"), | |
| 65: ("βοΈ", "Heavy Rain"), | |
| 71: ("π¨οΈ", "Slight Snow"), | |
| 73: ("π¨οΈ", "Moderate Snow"), | |
| 75: ("βοΈ", "Heavy Snow"), | |
| 80: ("π§οΈ", "Slight Rain Showers"), | |
| 81: ("π§οΈ", "Moderate Rain Showers"), | |
| 82: ("βοΈ", "Violent Rain Showers"), | |
| 95: ("β‘", "Thunderstorm"), | |
| 96: ("β‘", "Thunderstorm with slight hail"), | |
| 99: ("β‘", "Thunderstorm with heavy hail"), | |
| } | |
| return codes.get(code, ("β", "Unknown")) | |
| # 2. Main Function to fetch data | |
| def get_weather_data(city_name): | |
| if not city_name: | |
| return "Please enter a city name.", "", "", "", "" | |
| try: | |
| # Step A: Geocoding (Convert City Name to Latitude/Longitude) | |
| geo_url = "https://geocoding-api.open-meteo.com/v1/search" | |
| geo_params = {"name": city_name, "count": 1, "language": "en", "format": "json"} | |
| geo_res = requests.get(geo_url, params=geo_params).json() | |
| if not geo_res.get("results"): | |
| return f"β Could not find city: '{city_name}'", "-", "-", "-", "-" | |
| location = geo_res["results"][0] | |
| lat = location["latitude"] | |
| lon = location["longitude"] | |
| city_display = f"{location['name']}, {location.get('country', '')}" | |
| # Step B: Get Weather Data (Current conditions) | |
| # We request: temp, relative humidity, apparent temp (feels like), weather code, wind speed | |
| weather_url = "https://api.open-meteo.com/v1/forecast" | |
| weather_params = { | |
| "latitude": lat, | |
| "longitude": lon, | |
| "current": ["temperature_2m", "relative_humidity_2m", "apparent_temperature", "weather_code", "wind_speed_10m"], | |
| "wind_speed_unit": "kmh" | |
| } | |
| w_res = requests.get(weather_url, params=weather_params).json() | |
| current = w_res["current"] | |
| # Step C: Format Data | |
| temp = f"{current['temperature_2m']}Β°C" | |
| feels_like = f"{current['apparent_temperature']}Β°C" | |
| humidity = f"{current['relative_humidity_2m']}%" | |
| wind = f"{current['wind_speed_10m']} km/h" | |
| icon, description = get_weather_description(current["weather_code"]) | |
| status_text = f"{icon} {description}" | |
| # Return all values to fill the UI boxes | |
| return f"π Weather in {city_display}", temp, feels_like, status_text, f"π§ Humidity: {humidity} | π¨ Wind: {wind}" | |
| except Exception as e: | |
| return f"β οΈ Error: {str(e)}", "-", "-", "-", "-" | |
| # 3. Build the Professional UI using Blocks | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| """ | |
| # π Global Weather Tracker | |
| ### Real-time weather data powered by Open-Meteo | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| city_input = gr.Textbox( | |
| label="Search City", | |
| placeholder="Type city name (e.g. Lahore, Tokyo)...", | |
| lines=1 | |
| ) | |
| search_btn = gr.Button("Get Weather", variant="primary") | |
| with gr.Column(scale=2): | |
| # Header for the result | |
| location_output = gr.Markdown("### π Enter a city to begin") | |
| with gr.Row(): | |
| temp_output = gr.Textbox(label="Temperature", value="-") | |
| feels_output = gr.Textbox(label="Feels Like", value="-") | |
| with gr.Row(): | |
| condition_output = gr.Textbox(label="Condition", value="-") | |
| details_output = gr.Textbox(label="Details", value="-") | |
| # Connect the button to the function | |
| search_btn.click( | |
| fn=get_weather_data, | |
| inputs=city_input, | |
| outputs=[location_output, temp_output, feels_output, condition_output, details_output] | |
| ) | |
| # Allow pressing "Enter" to search | |
| city_input.submit( | |
| fn=get_weather_data, | |
| inputs=city_input, | |
| outputs=[location_output, temp_output, feels_output, condition_output, details_output] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |