nouamanetazi's picture
nouamanetazi HF staff
big refactoring
597fb2d
raw
history blame
6.1 kB
import os
import time
from src.markers import display_interventions, display_solved, show_requests, show_verified_requests
from src.filters import show_requests_filters, show_interventions_filters
import folium
import pandas as pd
import streamlit as st
from huggingface_hub import HfApi
from streamlit_folium import st_folium
from src.components import show_dataframes_metrics, show_embed_code, id_review_submission, show_donations
from src.text_content import (
CREDITS_TEXT,
LOGO,
SLOGAN,
)
from src.utils import add_latlng_col, init_map, parse_gg_sheet, is_request_in_list, parse_json_file
from src.map_utils import get_legend_macro
from src.dataframes import display_dataframe
TOKEN = os.environ.get("HF_TOKEN", None)
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"
api = HfApi(TOKEN)
# Initialize Streamlit Config
st.set_page_config(
layout="wide",
initial_sidebar_state="collapsed",
page_icon="🤝",
page_title="Nt3awnou نتعاونو",
)
# Initialize States
if "sleep_time" not in st.session_state:
st.session_state.sleep_time = 2
if "auto_refresh" not in st.session_state:
st.session_state.auto_refresh = False
auto_refresh = st.sidebar.checkbox("Auto Refresh?", st.session_state.auto_refresh)
if auto_refresh:
number = st.sidebar.number_input("Refresh rate in seconds", value=st.session_state.sleep_time)
st.session_state.sleep_time = number
# Streamlit functions
# Logo and Title
st.markdown(LOGO, unsafe_allow_html=True)
# st.title("Nt3awnou نتعاونو")
st.markdown(SLOGAN, unsafe_allow_html=True)
m, emergency_fgs, intervention_fgs = init_map()
fg = folium.FeatureGroup(name="Markers")
# Selection of requests
selected_options, options, show_unverified, show_interventions = show_requests_filters()
# Load data and initialize map with plugins
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_verified_requests = len(verified_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))
]
# Selection of interventions
(
selected_statuses,
critical_villages,
partially_satisfied_villages,
fully_satisfied_villages,
) = show_interventions_filters()
# Add interventions markers to map
if show_interventions:
display_solved(solved_verified_requests, selected_statuses, fg)
display_interventions(interventions_df, selected_statuses, m, intervention_fgs)
# Add requests markers to map
if show_unverified:
show_requests(filtered_df, fg)
# Add verified requests markers to map
show_verified_requests(verified_df, emergency_fgs)
# Add legend
legend_macro = get_legend_macro(show_unverified)
# delete old legend
for child in m.get_root()._children:
pass # TODO: fix this
# if child.startswith("macro_element"):
# m.get_root()._children.remove(child)
m.get_root().add_child(legend_macro)
# add_village_names(douar_df, m)
st_folium(m, use_container_width=True, returned_objects=[], feature_group_to_add=fg, key="map")
# Embed code
show_embed_code()
# Show metrics
show_dataframes_metrics(len_requests, len_interventions, len_solved_verified_requests)
# Verified Requests table
st.divider()
st.subheader("📝 **Table of verified requests / جدول الطلبات المؤكدة**")
drop_cols = [
"Phone Number",
"id",
"Status",
"Intervenant ",
"Intervention Date",
"Any remarks",
"VerificationStatus",
"Automatic Extracted Coordinates",
]
display_dataframe(
verified_df, drop_cols, VERIFIED_REQUESTS_URL, search_id=True, for_help_requests=True, show_link=False
)
# Requests table
st.divider()
st.subheader("📝 **Table of requests / جدول الطلبات**")
drop_cols = [
"(عند الامكان) رقم هاتف شخص موجود في عين المكان",
"الرجاء الضغط على الرابط التالي لمعرفة موقعك إذا كان متاحا",
"GeoStamp",
"GeoCode",
"GeoAddress",
"Status",
"id",
]
display_dataframe(filtered_df, drop_cols, REQUESTS_URL, search_id=True, for_help_requests=True)
# Interventions table
st.divider()
st.subheader("📝 **Table of interventions / جدول التدخلات**")
display_dataframe(
interventions_df,
[], # We show NGOs contact information
INTERVENTIONS_URL,
search_id=False,
status=True,
for_help_requests=False,
)
# Submit an id for review
st.divider()
id_review_submission(api)
# Donations can be made to the gouvernmental fund under the name
st.divider()
show_donations()
# Credits
st.markdown(
CREDITS_TEXT,
unsafe_allow_html=True,
)
if auto_refresh:
time.sleep(number)
st.experimental_rerun()