Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import streamlit as st | |
from src.utils import add_latlng_col, parse_gg_sheet, parse_json_file, is_request_in_list | |
import pandas as pd | |
VERIFIED_REQUESTS_URL = ( | |
"https://docs.google.com/spreadsheets/d/1PXcAtI5L95hHSXAiRl3Y4v5O4coG39S86OTfBEcvLTE/edit#gid=0" | |
) | |
REQUESTS_URL = "https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/edit#gid=966953708" | |
INTERVENTIONS_URL = ( | |
"https://docs.google.com/spreadsheets/d/1eXOTqunOWWP8FRdENPs4cU9ulISm4XZWYJJNR1-SrwY/edit#gid=2089222765" | |
) | |
DOUARS_URL = "data/regions.json" | |
def load_data(show_unverified, selected_options, options): | |
df = parse_gg_sheet(REQUESTS_URL) | |
if show_unverified: | |
df = add_latlng_col(df, process_column=15) | |
interventions_df = parse_gg_sheet(INTERVENTIONS_URL) | |
interventions_df = add_latlng_col(interventions_df, process_column="Automatic Extracted Coordinates") | |
verified_df = parse_gg_sheet(VERIFIED_REQUESTS_URL) | |
verified_df = add_latlng_col(verified_df, process_column="Automatic Extracted Coordinates") | |
douar_df = parse_json_file(DOUARS_URL) | |
# check if verified requests have been solved | |
solved_verified_requests = verified_df[~pd.isnull(verified_df["Status"])] | |
verified_df = verified_df[pd.isnull(verified_df["Status"])] | |
len_requests = len(df) | |
len_interventions = len(interventions_df) | |
len_solved_verified_requests = len(solved_verified_requests) | |
df["id"] = df.index # Needed to display request id | |
verified_df["id"] = verified_df.index # Needed to display request id | |
# keep rows with at least one request in selected_options | |
filtered_df = df[ | |
df["ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)"].apply(lambda x: is_request_in_list(x, selected_options, options)) | |
] | |
filtered_verified_df = verified_df[ | |
verified_df["Help Details"].apply(lambda x: is_request_in_list(x, selected_options, options)) | |
] | |
return ( | |
df, | |
filtered_df, | |
interventions_df, | |
verified_df, | |
filtered_verified_df, | |
solved_verified_requests, | |
douar_df, | |
len_requests, | |
len_interventions, | |
len_solved_verified_requests, | |
) | |
def display_dataframe(df, drop_cols, data_url, search_id=True, status=False, for_help_requests=False, show_link=True): | |
"""Display the dataframe in a table""" | |
col_1, col_2 = st.columns([1, 1]) | |
# has df's first row | |
df_hash = hash(df.iloc[0].to_string()) | |
with col_1: | |
query = st.text_input("🔍 Search for information / بحث عن المعلومات", key=f"query_{df_hash}") | |
with col_2: | |
if search_id: | |
id_number = st.number_input( | |
"🔍 Search for an id / بحث عن رقم", | |
min_value=0, | |
# max_value=len(df), | |
value=0, | |
step=1, | |
key=f"id_{df_hash}", | |
) | |
if status: | |
selected_status = st.selectbox( | |
"🗓️ Status / حالة", ["all / الكل", "Done / تم", "Planned / مخطط لها"], key=f"status_{df_hash}" | |
) | |
if query: | |
# Filtering the dataframe based on the query | |
mask = df.apply(lambda row: row.astype(str).str.contains(query.lower(), case=False).any(), axis=1) | |
display_df = df[mask] | |
else: | |
display_df = df | |
if search_id and id_number: | |
display_df = display_df[display_df["id"] == id_number] | |
display_df = display_df.drop(drop_cols, axis=1) | |
if status: | |
target = "Intervention status" | |
if selected_status == "Done / تم": | |
display_df = display_df[display_df[target] == "Intervention déjà passée / Past intevention"] | |
elif selected_status == "Planned / مخطط لها": | |
display_df = display_df[display_df[target] != "Intervention déjà passée / Past intevention"] | |
st.dataframe(display_df, height=500) | |
# Original link to the Google Sheet | |
if show_link: | |
st.markdown( | |
f"To view the full Google Sheet for advanced filtering go to: {data_url} **لعرض الورقة كاملة، اذهب إلى**" | |
) | |
# if we want to check hidden contact information | |
if for_help_requests: | |
st.markdown( | |
"We are hiding contact information to protect the privacy of the victims. If you are an NGO and want to contact the victims, please contact us at [email protected]", | |
) | |
st.markdown( | |
""" | |
<div style="text-align: left;"> | |
<a href="mailto:[email protected]">[email protected]</a> نحن نخفي معلومات الاتصال لحماية خصوصية الضحايا. إذا كنت جمعية وتريد الاتصال بالضحايا، يرجى الاتصال بنا على | |
</div> | |
""", | |
unsafe_allow_html=True, | |
) | |