fix issue in requests filtering (we didn't support multiple requests for the same entry)

#21
by loubnabnl HF staff - opened
Files changed (2) hide show
  1. app.py +10 -6
  2. src/utils.py +26 -17
app.py CHANGED
@@ -20,7 +20,7 @@ from src.text_content import (
20
  REVIEW_TEXT,
21
  SLOGAN,
22
  )
23
- from src.utils import add_latlng_col, init_map, parse_gg_sheet
24
 
25
  TOKEN = os.environ.get("HF_TOKEN", None)
26
  REQUESTS_URL = "https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/edit#gid=966953708"
@@ -97,12 +97,14 @@ def show_requests(filtered_df):
97
  """Display victim requests on the map"""
98
  for index, row in filtered_df.iterrows():
99
  request_type = row["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"]
 
100
  long_lat = row[
101
  "هل يمكنك تقديم الإحداثيات الدقيقة للموقع؟ (ادا كنت لا توجد بعين المكان) متلاً \n31.01837503440344, -6.781405948842175"
102
  ]
103
  maps_url = f"https://maps.google.com/?q={long_lat}"
 
104
  display_text = f'<b>Request Type:</b> {request_type}<br><b>Id:</b> {row["id"]}<br><a href="{maps_url}" target="_blank" rel="noopener noreferrer"><b>Google Maps</b></a>'
105
- icon_name = ICON_MAPPING.get(request_type, "info-sign")
106
  if row["latlng"] is None:
107
  continue
108
 
@@ -113,7 +115,7 @@ def show_requests(filtered_df):
113
  else None,
114
  popup=folium.Popup(display_text, max_width=300),
115
  icon=folium.Icon(
116
- color=COLOR_MAPPING.get(request_type, "blue"), icon=icon_name
117
  ),
118
  ))
119
 
@@ -258,8 +260,11 @@ for i, option in enumerate(options):
258
  selected_options.append(option)
259
 
260
  df["id"] = df.index
261
- filtered_df = df[df["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"].isin(selected_options)]
262
- selected_headers = [HEADERS_MAPPING[request] for request in selected_options]
 
 
 
263
 
264
  # Selection of interventions
265
  show_interventions = st.checkbox(
@@ -268,7 +273,6 @@ show_interventions = st.checkbox(
268
  )
269
 
270
  # Categories of villages
271
-
272
  st.markdown(
273
  "👉 **State of villages visited by NGOs| Etat de villages visités par les ONGs | وضعية القرى التي زارتها الجمعيات**",
274
  unsafe_allow_html=True,
 
20
  REVIEW_TEXT,
21
  SLOGAN,
22
  )
23
+ from src.utils import add_latlng_col, init_map, parse_gg_sheet, is_request_in_list, marker_request
24
 
25
  TOKEN = os.environ.get("HF_TOKEN", None)
26
  REQUESTS_URL = "https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/edit#gid=966953708"
 
97
  """Display victim requests on the map"""
98
  for index, row in filtered_df.iterrows():
99
  request_type = row["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"]
100
+ displayed_request = marker_request(request_type)
101
  long_lat = row[
102
  "هل يمكنك تقديم الإحداثيات الدقيقة للموقع؟ (ادا كنت لا توجد بعين المكان) متلاً \n31.01837503440344, -6.781405948842175"
103
  ]
104
  maps_url = f"https://maps.google.com/?q={long_lat}"
105
+ # we display all requests in popup text and use the first one for the icon/color
106
  display_text = f'<b>Request Type:</b> {request_type}<br><b>Id:</b> {row["id"]}<br><a href="{maps_url}" target="_blank" rel="noopener noreferrer"><b>Google Maps</b></a>'
107
+ icon_name = ICON_MAPPING.get(displayed_request, "info-sign")
108
  if row["latlng"] is None:
109
  continue
110
 
 
115
  else None,
116
  popup=folium.Popup(display_text, max_width=300),
117
  icon=folium.Icon(
118
+ color=COLOR_MAPPING.get(displayed_request, "blue"), icon=icon_name
119
  ),
120
  ))
121
 
 
260
  selected_options.append(option)
261
 
262
  df["id"] = df.index
263
+ # keep rows with at least one request in selected_options
264
+ filtered_df = df[df["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"].apply(
265
+ lambda x: is_request_in_list(x, selected_options)
266
+ )]
267
+
268
 
269
  # Selection of interventions
270
  show_interventions = st.checkbox(
 
273
  )
274
 
275
  # Categories of villages
 
276
  st.markdown(
277
  "👉 **State of villages visited by NGOs| Etat de villages visités par les ONGs | وضعية القرى التي زارتها الجمعيات**",
278
  unsafe_allow_html=True,
src/utils.py CHANGED
@@ -12,19 +12,26 @@ def parse_gg_sheet(url):
12
  df = pd.read_csv(url, on_bad_lines="warn")
13
  return df
14
 
15
- # Session for Requests
16
- # session = requests.Session()
17
- # @st.cache_data(persist=True)
18
- # def parse_latlng_from_link(url):
19
- # try:
20
- # # extract latitude and longitude from gmaps link
21
- # if "@" not in url:
22
- # resp = session.head(url, allow_redirects=True)
23
- # url = resp.url
24
- # latlng = url.split("@")[1].split(",")[0:2]
25
- # return [float(latlng[0]), float(latlng[1])]
26
- # except Exception as e:
27
- # return None
 
 
 
 
 
 
 
28
 
29
  def add_latlng_col(df, process_column):
30
  """Add a latlng column to the dataframe"""
@@ -108,19 +115,21 @@ def init_map():
108
  max_bounds=True,
109
  )
110
  # Add a search bar to the map
111
- plugins.Geocoder(
112
  collapsed=False,
113
  position="topright",
114
  placeholder="Search | البحث",
115
- ).add_to(m)
 
116
 
117
  # Add Fullscreen button to the map
118
- plugins.Fullscreen(
119
  position="topright",
120
  title="Expand me | تكبير الخريطة",
121
  title_cancel="Exit me | تصغير الخريطة",
122
  force_separate_button=True,
123
- ).add_to(m)
 
124
 
125
  # Satellite View from Mapbox
126
  tileurl = "https://marocmap.ikiker.com/maroc/{z}/{x}/{y}.png"
 
12
  df = pd.read_csv(url, on_bad_lines="warn")
13
  return df
14
 
15
+
16
+ def is_request_in_list(request, selection_list):
17
+ if isinstance(request, float): # Check if the input is a float (like NaN)
18
+ return False
19
+ if "," in request:
20
+ all_requests = [r.strip() for r in request.split(",")]
21
+ else:
22
+ all_requests = [request]
23
+ return any([r in selection_list for r in all_requests])
24
+
25
+
26
+ def marker_request(request):
27
+ # in case of multiple requests we use the first one for the marker's icon
28
+ # requests are already sorted by priority from the form
29
+ try:
30
+ displayed_request = request.split(',')[0]
31
+ except:
32
+ displayed_request = request
33
+ return displayed_request
34
+
35
 
36
  def add_latlng_col(df, process_column):
37
  """Add a latlng column to the dataframe"""
 
115
  max_bounds=True,
116
  )
117
  # Add a search bar to the map
118
+ geocoder = plugins.Geocoder(
119
  collapsed=False,
120
  position="topright",
121
  placeholder="Search | البحث",
122
+ )
123
+ m.add_child(geocoder)
124
 
125
  # Add Fullscreen button to the map
126
+ fullscreen = plugins.Fullscreen(
127
  position="topright",
128
  title="Expand me | تكبير الخريطة",
129
  title_cancel="Exit me | تصغير الخريطة",
130
  force_separate_button=True,
131
+ )
132
+ m.add_child(fullscreen)
133
 
134
  # Satellite View from Mapbox
135
  tileurl = "https://marocmap.ikiker.com/maroc/{z}/{x}/{y}.png"