import streamlit as st import requests from dotenv import load_dotenv import os # Load environment variables from .env file load_dotenv() # Get OpenWeatherMap API key from environment variables API_KEY = os.getenv('OPENWEATHERMAP_API_KEY') # Function to get weather data def get_weather(city): url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric" response = requests.get(url) if response.status_code == 200: return response.json() else: return None # Function to get the appropriate weather icon def get_weather_icon(weather_description): if 'rain' in weather_description: return 'fa-cloud-showers-heavy' elif 'clear' in weather_description: return 'fa-sun' elif 'cloud' in weather_description: return 'fa-cloud' elif 'thunderstorm' in weather_description: return 'fa-bolt' elif 'snow' in weather_description: return 'fa-snowflake' else: return 'fa-smog' # Streamlit UI def main(): st.set_page_config(page_title="Current Weather App", page_icon=":sun_behind_rain_cloud:", layout="wide") st.markdown( """ """, unsafe_allow_html=True, ) st.markdown("

Current Weather App

", unsafe_allow_html=True) city = st.text_input('Enter City Name', 'New York').strip() if st.button('Get Weather'): weather = get_weather(city) if weather: weather_description = weather['weather'][0]['description'] weather_icon_class = get_weather_icon(weather_description) st.markdown(f"

City: {weather['name']}

", unsafe_allow_html=True) st.markdown(f"
", unsafe_allow_html=True) st.markdown(f"

Temperature: {weather['main']['temp']}°C

", unsafe_allow_html=True) st.markdown(f"

Weather: {weather_description.capitalize()}

", unsafe_allow_html=True) st.markdown(f"

Humidity: {weather['main']['humidity']}%

", unsafe_allow_html=True) st.markdown(f"

Wind Speed: {weather['wind']['speed']} m/s

", unsafe_allow_html=True) else: st.error("City not found or API error.") if __name__ == '__main__': main()