Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| # ✅ Your WeatherAPI key | |
| API_KEY = "8b2b38749aac4addb48112739250107" | |
| BASE_URL = "http://api.weatherapi.com/v1" | |
| # Page config | |
| st.set_page_config(page_title="Haal-E-Mousam", page_icon="⛅") | |
| st.title("🌦️ Haal-E-Mousam") | |
| # Input field | |
| city = st.text_input("Enter city name:", placeholder="e.g. Lahore, New York, Tokyo") | |
| # On city input | |
| if city: | |
| endpoint = f"{BASE_URL}/forecast.json" | |
| params = { | |
| "key": API_KEY, | |
| "q": city, | |
| "days": 3, # Forecast for 3 days | |
| "aqi": "no", | |
| "alerts": "yes" | |
| } | |
| response = requests.get(endpoint, params=params) | |
| if response.status_code == 200: | |
| data = response.json() | |
| location = data["location"] | |
| current = data["current"] | |
| forecast = data["forecast"]["forecastday"] | |
| alerts = data.get("alerts", {}).get("alert", []) | |
| # Location and current weather | |
| st.subheader(f"📍 {location['name']}, {location['country']}") | |
| st.metric("🌡️ Temp", f"{current['temp_c']}°C", f"Feels like {current['feelslike_c']}°C") | |
| st.metric("💧 Humidity", f"{current['humidity']}%") | |
| st.metric("🌬️ Wind", f"{current['wind_kph']} kph") | |
| st.write(f"☁️ Condition: {current['condition']['text']}") | |
| # Alerts if any | |
| if alerts: | |
| st.subheader("⚠️ Weather Alerts") | |
| for alert in alerts: | |
| st.warning(f"**{alert['headline']}**\n\n{alert['desc']}") | |
| else: | |
| st.info("✅ No active weather alerts.") | |
| # Forecast | |
| st.subheader("📅 3-Day Forecast") | |
| for day in forecast: | |
| date = day["date"] | |
| avg_temp = day["day"]["avgtemp_c"] | |
| condition = day["day"]["condition"]["text"] | |
| max_temp = day["day"]["maxtemp_c"] | |
| min_temp = day["day"]["mintemp_c"] | |
| st.write( | |
| f"📆 **{date}** \n" | |
| f"🌡️ Avg: {avg_temp}°C (Max: {max_temp}°C / Min: {min_temp}°C) \n" | |
| f"☁️ Condition: {condition}" | |
| ) | |
| else: | |
| st.error("❌ Failed to fetch weather. Check city name or try again later.") | |