Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,43 +23,62 @@ if 'location_name' not in st.session_state:
|
|
| 23 |
default_date = datetime.utcnow().date()
|
| 24 |
selected_date = st.text_input("Date (YYYY-MM-DD)", value=str(default_date))
|
| 25 |
|
| 26 |
-
# Layout:
|
| 27 |
col1, col2, col3 = st.columns(3)
|
| 28 |
|
| 29 |
with col1:
|
| 30 |
-
|
| 31 |
with col2:
|
| 32 |
-
|
| 33 |
with col3:
|
| 34 |
location_search = st.text_input("Search Location (English only)", value=st.session_state['location_name'])
|
| 35 |
|
| 36 |
# Search button
|
| 37 |
if st.button("Search"):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
location = geolocator.geocode(location_search, language="en")
|
| 42 |
if location:
|
| 43 |
st.session_state['latitude'] = location.latitude
|
| 44 |
st.session_state['longitude'] = location.longitude
|
| 45 |
st.session_state['location_name'] = location.address
|
| 46 |
st.success(f"β
Location found: {location.address}")
|
|
|
|
| 47 |
else:
|
| 48 |
-
st.error("β Location not found.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Map
|
| 54 |
-
st.subheader("Map View:")
|
| 55 |
|
| 56 |
# Create Map
|
| 57 |
-
m = folium.Map(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
# Add marker
|
| 60 |
folium.Marker(
|
| 61 |
[st.session_state['latitude'], st.session_state['longitude']],
|
| 62 |
-
popup=st.session_state['location_name'] or
|
| 63 |
tooltip="Click for location",
|
| 64 |
).add_to(m)
|
| 65 |
|
|
@@ -80,4 +99,5 @@ if st_data and st_data.get("last_clicked"):
|
|
| 80 |
st.session_state['location_name'] = ""
|
| 81 |
except:
|
| 82 |
st.session_state['location_name'] = ""
|
| 83 |
-
st.experimental_rerun()
|
|
|
|
|
|
| 23 |
default_date = datetime.utcnow().date()
|
| 24 |
selected_date = st.text_input("Date (YYYY-MM-DD)", value=str(default_date))
|
| 25 |
|
| 26 |
+
# Layout: 3 columns for inputs
|
| 27 |
col1, col2, col3 = st.columns(3)
|
| 28 |
|
| 29 |
with col1:
|
| 30 |
+
latitude_input = st.text_input("Latitude", value=str(st.session_state['latitude']))
|
| 31 |
with col2:
|
| 32 |
+
longitude_input = st.text_input("Longitude", value=str(st.session_state['longitude']))
|
| 33 |
with col3:
|
| 34 |
location_search = st.text_input("Search Location (English only)", value=st.session_state['location_name'])
|
| 35 |
|
| 36 |
# Search button
|
| 37 |
if st.button("Search"):
|
| 38 |
+
with st.spinner("π Searching and updating the location..."):
|
| 39 |
+
# First try location search
|
| 40 |
+
if location_search.strip() != "":
|
| 41 |
location = geolocator.geocode(location_search, language="en")
|
| 42 |
if location:
|
| 43 |
st.session_state['latitude'] = location.latitude
|
| 44 |
st.session_state['longitude'] = location.longitude
|
| 45 |
st.session_state['location_name'] = location.address
|
| 46 |
st.success(f"β
Location found: {location.address}")
|
| 47 |
+
st.balloons()
|
| 48 |
else:
|
| 49 |
+
st.error("β Location not found. Please try another keyword.")
|
| 50 |
+
# If no location given, fall back to lat/lon
|
| 51 |
+
else:
|
| 52 |
+
try:
|
| 53 |
+
lat = float(latitude_input)
|
| 54 |
+
lon = float(longitude_input)
|
| 55 |
+
location = geolocator.reverse((lat, lon), language="en")
|
| 56 |
+
if location:
|
| 57 |
+
st.session_state['location_name'] = location.address
|
| 58 |
+
st.success(f"β
Coordinates updated to: {location.address}")
|
| 59 |
+
st.balloons()
|
| 60 |
+
else:
|
| 61 |
+
st.warning("β οΈ Valid coordinates but no address found.")
|
| 62 |
+
st.session_state['latitude'] = lat
|
| 63 |
+
st.session_state['longitude'] = lon
|
| 64 |
+
except ValueError:
|
| 65 |
+
st.error("β Invalid latitude or longitude format.")
|
| 66 |
|
| 67 |
# Map
|
| 68 |
+
st.subheader("πΊοΈ Map View:")
|
| 69 |
|
| 70 |
# Create Map
|
| 71 |
+
m = folium.Map(
|
| 72 |
+
location=[st.session_state['latitude'], st.session_state['longitude']],
|
| 73 |
+
zoom_start=5,
|
| 74 |
+
control_scale=True,
|
| 75 |
+
tiles="OpenStreetMap"
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
# Add marker in English popup
|
| 79 |
folium.Marker(
|
| 80 |
[st.session_state['latitude'], st.session_state['longitude']],
|
| 81 |
+
popup=f"π {st.session_state['location_name'] or 'Selected Location'}",
|
| 82 |
tooltip="Click for location",
|
| 83 |
).add_to(m)
|
| 84 |
|
|
|
|
| 99 |
st.session_state['location_name'] = ""
|
| 100 |
except:
|
| 101 |
st.session_state['location_name'] = ""
|
| 102 |
+
st.experimental_rerun()
|
| 103 |
+
|