Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
•
e5a285a
1
Parent(s):
181f4c3
add "show unverified requests"
Browse files- app.py +79 -22
- src/utils.py +1 -1
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import time
|
3 |
from datetime import datetime
|
|
|
4 |
|
5 |
import folium
|
6 |
import pandas as pd
|
@@ -23,6 +24,7 @@ from src.text_content import (
|
|
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"
|
27 |
INTERVENTIONS_URL = "https://docs.google.com/spreadsheets/d/1eXOTqunOWWP8FRdENPs4cU9ulISm4XZWYJJNR1-SrwY/edit#gid=2089222765"
|
28 |
api = HfApi(TOKEN)
|
@@ -53,6 +55,7 @@ if auto_refresh:
|
|
53 |
# Streamlit functions
|
54 |
def display_interventions(interventions_df, selected_statuses):
|
55 |
"""Display NGO interventions on the map"""
|
|
|
56 |
for index, row in interventions_df.iterrows():
|
57 |
village_status = row[interventions_df.columns[7]]
|
58 |
is_future_intervention = row[interventions_df.columns[5]] == "Intervention prévue dans le futur / Planned future intervention"
|
@@ -97,30 +100,53 @@ def display_interventions(interventions_df, selected_statuses):
|
|
97 |
|
98 |
def show_requests(filtered_df):
|
99 |
"""Display victim requests on the map"""
|
|
|
100 |
for index, row in filtered_df.iterrows():
|
101 |
request_type = row["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"]
|
102 |
displayed_request = marker_request(request_type) # TODO: the marker should depend on selected_options
|
103 |
-
long_lat = row[
|
104 |
-
"هل يمكنك تقديم الإحداثيات الدقيقة للموقع؟ (ادا كنت لا توجد بعين المكان) متلاً \n31.01837503440344, -6.781405948842175"
|
105 |
-
]
|
106 |
maps_url = f"https://maps.google.com/?q={long_lat}"
|
107 |
# we display all requests in popup text and use the first one for the icon/color
|
108 |
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>'
|
109 |
icon_name = ICON_MAPPING.get(request_type, "list")
|
110 |
-
if
|
111 |
continue
|
112 |
|
113 |
fg.add_child(folium.Marker(
|
114 |
-
location=
|
115 |
tooltip=row[" لأي جماعة / قيادة / دوار تنتمون ؟"]
|
116 |
if not pd.isna(row[" لأي جماعة / قيادة / دوار تنتمون ؟"])
|
117 |
else None,
|
118 |
popup=folium.Popup(display_text, max_width=300),
|
119 |
icon=folium.Icon(
|
120 |
-
color=COLOR_MAPPING.get(displayed_request, "
|
121 |
),
|
122 |
))
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
def display_google_sheet_tables(data_url):
|
126 |
"""Display the google sheet tables for requests and interventions"""
|
@@ -137,7 +163,7 @@ def display_dataframe(df, drop_cols, data_url, search_id=True, status=False, for
|
|
137 |
with col_1:
|
138 |
query = st.text_input(
|
139 |
"🔍 Search for information / بحث عن المعلومات",
|
140 |
-
key=f"
|
141 |
)
|
142 |
with col_2:
|
143 |
if search_id:
|
@@ -147,12 +173,13 @@ def display_dataframe(df, drop_cols, data_url, search_id=True, status=False, for
|
|
147 |
max_value=len(filtered_df),
|
148 |
value=0,
|
149 |
step=1,
|
|
|
150 |
)
|
151 |
if status:
|
152 |
selected_status = st.selectbox(
|
153 |
"🗓️ Status / حالة",
|
154 |
["all / الكل", "Done / تم", "Planned / مخطط لها"],
|
155 |
-
key="
|
156 |
)
|
157 |
|
158 |
if query:
|
@@ -231,13 +258,6 @@ st.markdown(LOGO, unsafe_allow_html=True)
|
|
231 |
# st.title("Nt3awnou نتعاونو")
|
232 |
st.markdown(SLOGAN, unsafe_allow_html=True)
|
233 |
|
234 |
-
# Load data and initialize map with plugins
|
235 |
-
df = parse_gg_sheet(REQUESTS_URL)
|
236 |
-
df = add_latlng_col(df, process_column=15)
|
237 |
-
len_requests = len(df)
|
238 |
-
interventions_df = parse_gg_sheet(INTERVENTIONS_URL)
|
239 |
-
interventions_df = add_latlng_col(interventions_df, process_column="Automatic Extracted Coordinates")
|
240 |
-
len_interventions = len(interventions_df)
|
241 |
m = init_map()
|
242 |
fg = folium.FeatureGroup(name="Markers")
|
243 |
|
@@ -251,7 +271,18 @@ options = [
|
|
251 |
]
|
252 |
selected_options = []
|
253 |
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
st.markdown(
|
256 |
"👉 **Choose request type | Choissisez le type de demande | اختر نوع الطلب**"
|
257 |
)
|
@@ -263,18 +294,30 @@ for i, option in enumerate(options):
|
|
263 |
if checked:
|
264 |
selected_options.append(option)
|
265 |
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
# keep rows with at least one request in selected_options
|
268 |
filtered_df = df[df["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"].apply(
|
269 |
lambda x: is_request_in_list(x, selected_options, options)
|
270 |
)]
|
|
|
|
|
|
|
271 |
|
272 |
|
273 |
# Selection of interventions
|
274 |
-
show_interventions = st.checkbox(
|
275 |
-
"Display Interventions | Afficher les interventions | عرض عمليات المساعدة",
|
276 |
-
value=True,
|
277 |
-
)
|
278 |
|
279 |
st.markdown(
|
280 |
"👉 **State of villages visited by NGOs| Etat de villages visités par les ONGs | وضعية القرى التي زارتها الجمعيات**",
|
@@ -318,7 +361,11 @@ if show_interventions:
|
|
318 |
display_interventions(interventions_df, selected_statuses)
|
319 |
|
320 |
# Show requests
|
321 |
-
|
|
|
|
|
|
|
|
|
322 |
|
323 |
st_folium(m, use_container_width=True, returned_objects=[], feature_group_to_add=fg, key="map")
|
324 |
|
@@ -416,6 +463,16 @@ with tab_fr:
|
|
416 |
unsafe_allow_html=True,
|
417 |
)
|
418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
# Requests table
|
420 |
st.divider()
|
421 |
st.subheader("📝 **Table of requests / جدول الطلبات**")
|
|
|
1 |
import os
|
2 |
import time
|
3 |
from datetime import datetime
|
4 |
+
import uuid
|
5 |
|
6 |
import folium
|
7 |
import pandas as pd
|
|
|
24 |
from src.utils import add_latlng_col, init_map, parse_gg_sheet, is_request_in_list, marker_request
|
25 |
|
26 |
TOKEN = os.environ.get("HF_TOKEN", None)
|
27 |
+
VERIFIED_REQUESTS_URL = "https://docs.google.com/spreadsheets/d/1PXcAtI5L95hHSXAiRl3Y4v5O4coG39S86OTfBEcvLTE/edit#gid=0"
|
28 |
REQUESTS_URL = "https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/edit#gid=966953708"
|
29 |
INTERVENTIONS_URL = "https://docs.google.com/spreadsheets/d/1eXOTqunOWWP8FRdENPs4cU9ulISm4XZWYJJNR1-SrwY/edit#gid=2089222765"
|
30 |
api = HfApi(TOKEN)
|
|
|
55 |
# Streamlit functions
|
56 |
def display_interventions(interventions_df, selected_statuses):
|
57 |
"""Display NGO interventions on the map"""
|
58 |
+
global fg
|
59 |
for index, row in interventions_df.iterrows():
|
60 |
village_status = row[interventions_df.columns[7]]
|
61 |
is_future_intervention = row[interventions_df.columns[5]] == "Intervention prévue dans le futur / Planned future intervention"
|
|
|
100 |
|
101 |
def show_requests(filtered_df):
|
102 |
"""Display victim requests on the map"""
|
103 |
+
global fg
|
104 |
for index, row in filtered_df.iterrows():
|
105 |
request_type = row["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"]
|
106 |
displayed_request = marker_request(request_type) # TODO: the marker should depend on selected_options
|
107 |
+
long_lat = row["latlng"]
|
|
|
|
|
108 |
maps_url = f"https://maps.google.com/?q={long_lat}"
|
109 |
# we display all requests in popup text and use the first one for the icon/color
|
110 |
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>'
|
111 |
icon_name = ICON_MAPPING.get(request_type, "list")
|
112 |
+
if long_lat is None:
|
113 |
continue
|
114 |
|
115 |
fg.add_child(folium.Marker(
|
116 |
+
location=long_lat,
|
117 |
tooltip=row[" لأي جماعة / قيادة / دوار تنتمون ؟"]
|
118 |
if not pd.isna(row[" لأي جماعة / قيادة / دوار تنتمون ؟"])
|
119 |
else None,
|
120 |
popup=folium.Popup(display_text, max_width=300),
|
121 |
icon=folium.Icon(
|
122 |
+
color=COLOR_MAPPING.get(displayed_request, "beige"), icon=icon_name, prefix="glyphicon"
|
123 |
),
|
124 |
))
|
125 |
|
126 |
+
def show_verified_requests(filtered_verified_df):
|
127 |
+
"""Display verified victim requests on the map"""
|
128 |
+
global fg
|
129 |
+
for index, row in filtered_verified_df.iterrows():
|
130 |
+
request_type = row["Help Details"]
|
131 |
+
displayed_request = marker_request(request_type) # TODO: the marker should depend on selected_options
|
132 |
+
long_lat = row["latlng"]
|
133 |
+
maps_url = f"https://maps.google.com/?q={long_lat}"
|
134 |
+
# we display all requests in popup text and use the first one for the icon/color
|
135 |
+
display_text = f'<b>Request Type:</b> {request_type}<br><b>Verified Id:</b> {row["id"]}<br><a href="{maps_url}" target="_blank" rel="noopener noreferrer"><b>Google Maps</b></a>'
|
136 |
+
icon_name = ICON_MAPPING.get(request_type, "list")
|
137 |
+
if long_lat is None:
|
138 |
+
continue
|
139 |
+
location = row['Location Details']
|
140 |
+
fg.add_child(folium.Marker(
|
141 |
+
location=long_lat,
|
142 |
+
tooltip=location
|
143 |
+
if not pd.isna(location)
|
144 |
+
else None,
|
145 |
+
popup=folium.Popup(display_text, max_width=300),
|
146 |
+
icon=folium.Icon(
|
147 |
+
color=COLOR_MAPPING.get(displayed_request, "beige"), icon=icon_name, prefix="glyphicon"
|
148 |
+
),
|
149 |
+
))
|
150 |
|
151 |
def display_google_sheet_tables(data_url):
|
152 |
"""Display the google sheet tables for requests and interventions"""
|
|
|
163 |
with col_1:
|
164 |
query = st.text_input(
|
165 |
"🔍 Search for information / بحث عن المعلومات",
|
166 |
+
key=f"query_{uuid.uuid4()}"
|
167 |
)
|
168 |
with col_2:
|
169 |
if search_id:
|
|
|
173 |
max_value=len(filtered_df),
|
174 |
value=0,
|
175 |
step=1,
|
176 |
+
key=f"id_{uuid.uuid4()}"
|
177 |
)
|
178 |
if status:
|
179 |
selected_status = st.selectbox(
|
180 |
"🗓️ Status / حالة",
|
181 |
["all / الكل", "Done / تم", "Planned / مخطط لها"],
|
182 |
+
key=f"status_{uuid.uuid4()}"
|
183 |
)
|
184 |
|
185 |
if query:
|
|
|
258 |
# st.title("Nt3awnou نتعاونو")
|
259 |
st.markdown(SLOGAN, unsafe_allow_html=True)
|
260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
m = init_map()
|
262 |
fg = folium.FeatureGroup(name="Markers")
|
263 |
|
|
|
271 |
]
|
272 |
selected_options = []
|
273 |
|
274 |
+
col1, col2 = st.columns([1, 1])
|
275 |
+
with col1:
|
276 |
+
show_unverified = st.checkbox(
|
277 |
+
"Display unverified requests / عرض الطلبات غير المؤكدة / Afficher les demandes non vérifiées",
|
278 |
+
value=False,
|
279 |
+
)
|
280 |
+
with col2:
|
281 |
+
show_interventions = st.checkbox(
|
282 |
+
"Display Interventions | Afficher les interventions | عرض عمليات المساعدة",
|
283 |
+
value=True,
|
284 |
+
)
|
285 |
+
|
286 |
st.markdown(
|
287 |
"👉 **Choose request type | Choissisez le type de demande | اختر نوع الطلب**"
|
288 |
)
|
|
|
294 |
if checked:
|
295 |
selected_options.append(option)
|
296 |
|
297 |
+
# Load data and initialize map with plugins
|
298 |
+
df = parse_gg_sheet(REQUESTS_URL)
|
299 |
+
if show_unverified:
|
300 |
+
df = add_latlng_col(df, process_column=15)
|
301 |
+
len_requests = len(df)
|
302 |
+
interventions_df = parse_gg_sheet(INTERVENTIONS_URL)
|
303 |
+
interventions_df = add_latlng_col(interventions_df, process_column="Automatic Extracted Coordinates")
|
304 |
+
len_interventions = len(interventions_df)
|
305 |
+
verified_df = parse_gg_sheet(VERIFIED_REQUESTS_URL)
|
306 |
+
verified_df = add_latlng_col(verified_df, process_column="Automatic Extracted Coordinates")
|
307 |
+
len_verified_requests = len(verified_df)
|
308 |
+
|
309 |
+
df["id"] = df.index # Needed to display request id
|
310 |
+
verified_df["id"] = verified_df.index # Needed to display request id
|
311 |
# keep rows with at least one request in selected_options
|
312 |
filtered_df = df[df["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"].apply(
|
313 |
lambda x: is_request_in_list(x, selected_options, options)
|
314 |
)]
|
315 |
+
filtered_verified_df = verified_df[verified_df["Help Details"].apply(
|
316 |
+
lambda x: is_request_in_list(x, selected_options, options)
|
317 |
+
)]
|
318 |
|
319 |
|
320 |
# Selection of interventions
|
|
|
|
|
|
|
|
|
321 |
|
322 |
st.markdown(
|
323 |
"👉 **State of villages visited by NGOs| Etat de villages visités par les ONGs | وضعية القرى التي زارتها الجمعيات**",
|
|
|
361 |
display_interventions(interventions_df, selected_statuses)
|
362 |
|
363 |
# Show requests
|
364 |
+
if show_unverified:
|
365 |
+
show_requests(filtered_df)
|
366 |
+
|
367 |
+
# Show verified requests
|
368 |
+
show_verified_requests(verified_df)
|
369 |
|
370 |
st_folium(m, use_container_width=True, returned_objects=[], feature_group_to_add=fg, key="map")
|
371 |
|
|
|
463 |
unsafe_allow_html=True,
|
464 |
)
|
465 |
|
466 |
+
# Verified Requests table
|
467 |
+
st.divider()
|
468 |
+
st.subheader("📝 **Table of verified requests / جدول الطلبات المؤكدة**")
|
469 |
+
drop_cols = [
|
470 |
+
"Phone Number",
|
471 |
+
"id",
|
472 |
+
"VerificationStatus"
|
473 |
+
]
|
474 |
+
display_dataframe(verified_df, drop_cols, VERIFIED_REQUESTS_URL, search_id=True, for_help_requests=True)
|
475 |
+
|
476 |
# Requests table
|
477 |
st.divider()
|
478 |
st.subheader("📝 **Table of requests / جدول الطلبات**")
|
src/utils.py
CHANGED
@@ -9,7 +9,7 @@ import streamlit as st
|
|
9 |
EPICENTER_LOCATION = [31.12210171476489, -8.42945837915193]
|
10 |
BORDER_COLOR = "black"
|
11 |
|
12 |
-
@st.
|
13 |
def parse_gg_sheet(url):
|
14 |
print("Parsing Google Sheet:", url)
|
15 |
url = url.replace("edit#gid=", "export?format=csv&gid=")
|
|
|
9 |
EPICENTER_LOCATION = [31.12210171476489, -8.42945837915193]
|
10 |
BORDER_COLOR = "black"
|
11 |
|
12 |
+
# @st.cache(persist=False) #TODO fix this
|
13 |
def parse_gg_sheet(url):
|
14 |
print("Parsing Google Sheet:", url)
|
15 |
url = url.replace("edit#gid=", "export?format=csv&gid=")
|