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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from geopy.distance import great_circle
|
| 4 |
+
import folium
|
| 5 |
+
from streamlit_folium import st_folium
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
def format_datetime(iso_string):
|
| 9 |
+
# Convert ISO 8601 string to datetime object
|
| 10 |
+
dt_object = datetime.fromisoformat(iso_string)
|
| 11 |
+
# Format as DD-MM-YYYY and 24-hour time
|
| 12 |
+
return dt_object.strftime("%d-%m-%Y %H:%M")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Expanded list of Indian cities with their latitude and longitude
|
| 16 |
+
cities = {
|
| 17 |
+
"Visakhapatnam": (17.6868, 83.2185),
|
| 18 |
+
"Delhi": (28.6139, 77.2090),
|
| 19 |
+
"Mumbai": (19.0760, 72.8777),
|
| 20 |
+
"Bangalore": (12.9716, 77.5946),
|
| 21 |
+
"Chennai": (13.0827, 80.2707),
|
| 22 |
+
"Kolkata": (22.5726, 88.3639),
|
| 23 |
+
"Hyderabad": (17.3850, 78.4867),
|
| 24 |
+
"Pune": (18.5204, 73.8567),
|
| 25 |
+
"Jaipur": (26.9124, 75.7873),
|
| 26 |
+
"Ahmedabad": (23.0225, 72.5714),
|
| 27 |
+
"Surat": (21.1702, 72.8311),
|
| 28 |
+
"Lucknow": (26.8467, 80.9462),
|
| 29 |
+
"Kanpur": (26.4499, 80.3319),
|
| 30 |
+
"Nagpur": (21.1458, 79.0882),
|
| 31 |
+
"Indore": (22.7196, 75.8577),
|
| 32 |
+
"Bhopal": (23.2599, 77.4126),
|
| 33 |
+
"Patna": (25.5941, 85.1376),
|
| 34 |
+
"Vadodara": (22.3072, 73.1812),
|
| 35 |
+
"Ludhiana": (30.9010, 75.8573),
|
| 36 |
+
"Agra": (27.1767, 78.0081),
|
| 37 |
+
"Nashik": (19.9975, 73.7898),
|
| 38 |
+
"Faridabad": (28.4089, 77.3178),
|
| 39 |
+
"Meerut": (28.9845, 77.7064),
|
| 40 |
+
"Rajkot": (22.3039, 70.8022),
|
| 41 |
+
"Varanasi": (25.3176, 82.9739),
|
| 42 |
+
"Srinagar": (34.0837, 74.7973),
|
| 43 |
+
"Amritsar": (31.6340, 74.8723),
|
| 44 |
+
"Navi Mumbai": (19.0330, 73.0297),
|
| 45 |
+
"Allahabad": (25.4358, 81.8463),
|
| 46 |
+
"Ranchi": (23.3441, 85.3096),
|
| 47 |
+
"Coimbatore": (11.0168, 76.9558),
|
| 48 |
+
"Jabalpur": (23.1815, 79.9864),
|
| 49 |
+
"Gwalior": (26.2183, 78.1828),
|
| 50 |
+
"Vijayawada": (16.5062, 80.6480),
|
| 51 |
+
"Jodhpur": (26.2389, 73.0243),
|
| 52 |
+
"Madurai": (9.9252, 78.1198),
|
| 53 |
+
"Raipur": (21.2514, 81.6296),
|
| 54 |
+
"Kota": (25.2138, 75.8648),
|
| 55 |
+
"Guwahati": (26.1445, 91.7362),
|
| 56 |
+
"Chandigarh": (30.7333, 76.7794),
|
| 57 |
+
"Solapur": (17.6599, 75.9064)
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
def get_weather(lat, lon):
|
| 61 |
+
# Using Open-Meteo API
|
| 62 |
+
url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 63 |
+
response = requests.get(url)
|
| 64 |
+
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
data = response.json()
|
| 67 |
+
current_weather = data['current_weather']
|
| 68 |
+
weather = {
|
| 69 |
+
'temperature': current_weather['temperature'],
|
| 70 |
+
'wind_speed': current_weather['windspeed'],
|
| 71 |
+
'wind_direction': current_weather['winddirection'],
|
| 72 |
+
'time': current_weather['time']
|
| 73 |
+
}
|
| 74 |
+
return weather
|
| 75 |
+
else:
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def find_nearest_city(lat, lon):
|
| 79 |
+
min_distance = float('inf')
|
| 80 |
+
nearest_city = None
|
| 81 |
+
for city, (city_lat, city_lon) in cities.items():
|
| 82 |
+
distance = great_circle((lat, lon), (city_lat, city_lon)).kilometers
|
| 83 |
+
if distance < min_distance:
|
| 84 |
+
min_distance = distance
|
| 85 |
+
nearest_city = city
|
| 86 |
+
return nearest_city
|
| 87 |
+
|
| 88 |
+
# Streamlit Layout
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
st.set_page_config(page_title="Indian Cities Weather", layout="wide")
|
| 93 |
+
|
| 94 |
+
st.title("🌤️ Indian Cities Weather App")
|
| 95 |
+
|
| 96 |
+
# Dropdown Menu
|
| 97 |
+
st.sidebar.subheader("Select a City")
|
| 98 |
+
selected_city = st.sidebar.selectbox("Choose a city:", list(cities.keys()))
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Show weather for selected city
|
| 103 |
+
if selected_city:
|
| 104 |
+
lat, lon = cities[selected_city]
|
| 105 |
+
weather = get_weather(lat, lon)
|
| 106 |
+
if weather:
|
| 107 |
+
st.markdown(f"### Weather in {selected_city}")
|
| 108 |
+
st.markdown(f"- 🌡️ Temperature: **{weather['temperature']}°C**")
|
| 109 |
+
st.markdown(f"- 💨 Wind Speed: **{weather['wind_speed']} km/h**")
|
| 110 |
+
st.markdown(f"- 🧭 Wind Direction: **{weather['wind_direction']}°**")
|
| 111 |
+
formatted_time = format_datetime(weather['time'])
|
| 112 |
+
st.markdown(f"- 🕒 Reported at: **{formatted_time}**")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Interactive Map with Clickable Markers
|
| 116 |
+
st.subheader("📍 Click on the Map or Select a City to Get Weather Information")
|
| 117 |
+
|
| 118 |
+
# Create a map with OpenStreetMap tiles for better compatibility
|
| 119 |
+
m = folium.Map(location=[22.9734, 78.6569], zoom_start=5, tiles='OpenStreetMap',
|
| 120 |
+
attr='Map data © OpenStreetMap contributors')
|
| 121 |
+
|
| 122 |
+
# Add markers with popups for each city
|
| 123 |
+
for city, (lat, lon) in cities.items():
|
| 124 |
+
weather = get_weather(lat, lon)
|
| 125 |
+
if weather:
|
| 126 |
+
popup_text = (f"<b>{city}</b><br>"
|
| 127 |
+
f"🌡️ Temp: {weather['temperature']}°C<br>"
|
| 128 |
+
f"💨 Wind: {weather['wind_speed']} km/h<br>"
|
| 129 |
+
f"🧭 Dir: {weather['wind_direction']}°<br>"
|
| 130 |
+
f"🕒 Time: {weather['time']}")
|
| 131 |
+
folium.Marker(
|
| 132 |
+
[lat, lon],
|
| 133 |
+
popup=folium.Popup(popup_text, max_width=250),
|
| 134 |
+
tooltip=city,
|
| 135 |
+
icon=folium.Icon(color='blue', icon='info-sign')
|
| 136 |
+
).add_to(m)
|
| 137 |
+
|
| 138 |
+
# Display map in Streamlit
|
| 139 |
+
clicked_location = st_folium(m, width=800, height=600)
|
| 140 |
+
|
| 141 |
+
# Get weather of nearest city when clicking on map
|
| 142 |
+
if clicked_location and clicked_location['last_clicked']:
|
| 143 |
+
lat = clicked_location['last_clicked']['lat']
|
| 144 |
+
lon = clicked_location['last_clicked']['lng']
|
| 145 |
+
nearest_city = find_nearest_city(lat, lon)
|
| 146 |
+
|
| 147 |
+
if nearest_city:
|
| 148 |
+
st.markdown(f"### Nearest City: **{nearest_city}**")
|
| 149 |
+
nearest_lat, nearest_lon = cities[nearest_city]
|
| 150 |
+
weather = get_weather(nearest_lat, nearest_lon)
|
| 151 |
+
if weather:
|
| 152 |
+
st.markdown(f"- 🌡️ Temperature: **{weather['temperature']}°C**")
|
| 153 |
+
st.markdown(f"- 💨 Wind Speed: **{weather['wind_speed']} km/h**")
|
| 154 |
+
st.markdown(f"- 🧭 Wind Direction: **{weather['wind_direction']}°**")
|
| 155 |
+
st.markdown(f"- 🕒 Reported at: **{weather['time']}**")
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
st.markdown("<style>body {background-color: #F0F8FF;}</style>", unsafe_allow_html=True)
|
| 159 |
+
|
| 160 |
+
# Footer
|
| 161 |
+
st.markdown(
|
| 162 |
+
"""
|
| 163 |
+
<div class="footer">
|
| 164 |
+
Developed by <b>SKB</b> | © 2025. All Rights Reserved
|
| 165 |
+
</div>
|
| 166 |
+
""",
|
| 167 |
+
unsafe_allow_html=True,
|
| 168 |
+
)
|
| 169 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.38.0
|
| 2 |
+
folium==0.19.4
|
| 3 |
+
geopy==2.4.1
|
| 4 |
+
Requests==2.32.3
|
| 5 |
+
streamlit_folium==0.24.0
|