Spaces:
No application file
No application file
| 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() |