Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import geocoder
|
| 4 |
+
|
| 5 |
+
# π Put your API key here
|
| 6 |
+
import os
|
| 7 |
+
API_KEY = os.getenv("API_KEY")
|
| 8 |
+
# -------- FUNCTIONS --------
|
| 9 |
+
def get_weather(city):
|
| 10 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
|
| 11 |
+
return requests.get(url).json()
|
| 12 |
+
|
| 13 |
+
def get_forecast(city):
|
| 14 |
+
url = f"http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={API_KEY}&units=metric"
|
| 15 |
+
return requests.get(url).json()
|
| 16 |
+
|
| 17 |
+
def get_location():
|
| 18 |
+
g = geocoder.ip('me')
|
| 19 |
+
return g.city
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# -------- PAGE CONFIG --------
|
| 23 |
+
st.set_page_config(page_title="π¦ Smart Weather App", layout="centered")
|
| 24 |
+
|
| 25 |
+
# -------- CUSTOM CSS (ENGAGING BACKGROUND) --------
|
| 26 |
+
st.markdown("""
|
| 27 |
+
<style>
|
| 28 |
+
body {
|
| 29 |
+
background: linear-gradient(135deg, #1e3c72, #2a5298);
|
| 30 |
+
color: white;
|
| 31 |
+
}
|
| 32 |
+
.main {
|
| 33 |
+
background: transparent;
|
| 34 |
+
}
|
| 35 |
+
.stTextInput>div>div>input {
|
| 36 |
+
border-radius: 10px;
|
| 37 |
+
}
|
| 38 |
+
.stButton button {
|
| 39 |
+
border-radius: 10px;
|
| 40 |
+
background-color: #00c6ff;
|
| 41 |
+
color: white;
|
| 42 |
+
}
|
| 43 |
+
</style>
|
| 44 |
+
""", unsafe_allow_html=True)
|
| 45 |
+
|
| 46 |
+
st.title("π¦ Smart Weather App")
|
| 47 |
+
|
| 48 |
+
# -------- SESSION STATE --------
|
| 49 |
+
if "city" not in st.session_state:
|
| 50 |
+
st.session_state.city = "Lahore" # default city
|
| 51 |
+
|
| 52 |
+
# -------- WEEK FORECAST (TOP) --------
|
| 53 |
+
city = st.session_state.city
|
| 54 |
+
forecast = get_forecast(city)
|
| 55 |
+
|
| 56 |
+
if forecast.get("cod") == "200":
|
| 57 |
+
st.subheader(f"π
Weekly Forecast - {city}")
|
| 58 |
+
|
| 59 |
+
cols = st.columns(5)
|
| 60 |
+
|
| 61 |
+
index = 0
|
| 62 |
+
for i in range(0, 40, 8):
|
| 63 |
+
data = forecast['list'][i]
|
| 64 |
+
|
| 65 |
+
with cols[index % 5]:
|
| 66 |
+
st.markdown(f"""
|
| 67 |
+
<div style='background: rgba(255,255,255,0.1); padding:10px; border-radius:10px; text-align:center'>
|
| 68 |
+
<b>{data['dt_txt'].split()[0]}</b><br>
|
| 69 |
+
π‘ {data['main']['temp']}Β°C<br>
|
| 70 |
+
β {data['weather'][0]['main']}
|
| 71 |
+
</div>
|
| 72 |
+
""", unsafe_allow_html=True)
|
| 73 |
+
|
| 74 |
+
index += 1
|
| 75 |
+
|
| 76 |
+
# -------- CURRENT WEATHER --------
|
| 77 |
+
weather = get_weather(city)
|
| 78 |
+
|
| 79 |
+
if weather.get("cod") == 200:
|
| 80 |
+
st.subheader("π Current Weather")
|
| 81 |
+
|
| 82 |
+
col1, col2, col3 = st.columns(3)
|
| 83 |
+
|
| 84 |
+
col1.metric("π‘ Temp", f"{weather['main']['temp']} Β°C")
|
| 85 |
+
col2.metric("π§ Humidity", f"{weather['main']['humidity']} %")
|
| 86 |
+
col3.metric("π₯ Condition", weather['weather'][0]['main'])
|
| 87 |
+
|
| 88 |
+
# -------- SEARCH BAR (BOTTOM) --------
|
| 89 |
+
st.markdown("---")
|
| 90 |
+
st.subheader("π Search City")
|
| 91 |
+
|
| 92 |
+
col1, col2 = st.columns([3,1])
|
| 93 |
+
|
| 94 |
+
with col1:
|
| 95 |
+
new_city = st.text_input("Enter city name", "")
|
| 96 |
+
|
| 97 |
+
with col2:
|
| 98 |
+
if st.button("π Auto"):
|
| 99 |
+
detected = get_location()
|
| 100 |
+
if detected:
|
| 101 |
+
st.session_state.city = detected
|
| 102 |
+
st.rerun()
|
| 103 |
+
|
| 104 |
+
# Manual search
|
| 105 |
+
if new_city:
|
| 106 |
+
st.session_state.city = new_city
|
| 107 |
+
st.rerun()
|