import os import requests from dotenv import load_dotenv import re import streamlit as st import pandas as pd #from st_aggrid import AgGrid, GridOptionsBuilder #from streamlit_echarts import st_echarts #from streamlit_chat import message #absolute path to the folder BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Load API key from .env file load_dotenv(dotenv_path=r"C:/Users/som/Desktop/Sharm/Study/Colleges/Duke/STudy/PYTHON/Assignments/my-local-repo/Week5/D2/Assignment/.gitignore/.env") API_KEY = os.getenv("OPENWEATHER_API_KEY") if not API_KEY: st.error("โŒ API Key not found! Make sure it's set in your .env file.") st.stop() def get_weather(city): # 1. Create the API endpoint URL base_url = "https://api.openweathermap.org/data/2.5/weather" # 2. Set query parameters params = { "q": city, "appid": API_KEY, "units": "metric" # temperature in Celsius } # 3. Make the request response = requests.get(base_url, params=params) if response.status_code == 200: #checks whether API call was successful(200 is the standard code for "OK"). # 4. Parse JSON data = response.json() # 5. Extract key info temp = data['main']['temp'] humidity = data['main']['humidity'] description = data['weather'][0]['description'] desc_lower = description.lower() # Advice suggestion logic if temp <= 10: img_file = os.path.join(BASE_DIR, "images", "chill.jpg") advice = "๐Ÿงฅ It's chilly โ€” wear a jacket." elif 10 < temp <= 20: if "rain" in desc_lower: img_file = os.path.join(BASE_DIR, "images", "shower.jpg") advice = "๐ŸŒง๏ธ Showers expected โ€” carry an umbrella." else: img_file = os.path.join(BASE_DIR, "images", "sun.jpg") advice = "๐Ÿงข Mild day โ€” a hat might be nice." else: # temp > 20 if "rain" in desc_lower: img_file = os.path.join(BASE_DIR, "images", "RAIN.jpg") advice = "โ˜” Warm but rainy โ€” take an umbrella." else: img_file = os.path.join(BASE_DIR, "images", "cloudy.jpg") advice = "๐Ÿ˜Ž Warm and clear โ€” enjoy the day!" # display image in col1, weather info in col2 col1, col2 = st.columns([1, 2]) with col1: st.image(img_file, use_container_width=True) with col2: #Highlight keywords using regex and ANSI bold st.markdown(f""" ### ๐Ÿ“ Weather in **{city.capitalize()}** - ๐ŸŒก๏ธ **Temperature:** {temp} ยฐC - ๐Ÿ’ง **Humidity:** {humidity}% - ๐Ÿ“ **Description:** {description.capitalize()} - ๐Ÿ”” **Suggestion:** {advice} """) else: st.error("โŒ Could not retrieve weather data. Please check the city name.") def main(): # 1. Title and description st.title(" ๐ŸŒ Weather Information App") st.markdown("---") st.write("Fetch and Display the current weather for any city in the world. ") with st.expander("โ„น๏ธ About this app"): st.write("The app uses the [OpenWeatherMap API](https://openweathermap.org/api) and gives practical suggestions based on the weather and temperature (e.g., bring a jacket or umbrella).") st.markdown("---") #st.image("images/weather.jpg", use_container_width=True) st.markdown("---") with st.container(): st.subheader("UserName") # Text input tied to session state key "username" name = st.text_input("Enter your name", key="username") # Use the stored value elsewhere if name: st.success(f"๐Ÿ‘‹ Hello, {st.session_state.username}!") st.markdown("---") st.title(" Stay informed about the weather ๐ŸŒฆ๏ธ ") st.write("*" *80) city = st.text_input("Enter the name of the city:") city = re.sub(r"[^a-zA-Z\s]", "", city).strip() if st.button("Get Weather"): if city: get_weather(city) #with st.spinner("Fetching weather..."): else: st.error("โŒ Please enter a city name before fetching weather.") if __name__ == "__main__": main()