Inam65 commited on
Commit
74b49cd
Β·
verified Β·
1 Parent(s): 0a0cb86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -15
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: 2 columns for inputs
27
  col1, col2, col3 = st.columns(3)
28
 
29
  with col1:
30
- latitude = st.text_input("Latitude", value=str(st.session_state['latitude']))
31
  with col2:
32
- longitude = 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
- if location_search:
39
- with st.spinner("πŸ”Ž Searching for location..."):
40
- try:
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. Try another keyword.")
49
- except Exception as e:
50
- st.error(f"❌ Error while searching the location: {e}")
51
-
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Map
54
- st.subheader("Map View:")
55
 
56
  # Create Map
57
- m = folium.Map(location=[st.session_state['latitude'], st.session_state['longitude']], zoom_start=5)
 
 
 
 
 
58
 
59
- # Add marker
60
  folium.Marker(
61
  [st.session_state['latitude'], st.session_state['longitude']],
62
- popup=st.session_state['location_name'] or "Selected Location",
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() # Rerun to update UI
 
 
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
+