File size: 6,095 Bytes
549a2cc
 
597fb2d
 
5c8a315
b10dbaa
 
549a2cc
5c8a315
 
597fb2d
5c8a315
 
 
 
 
597fb2d
a1551a6
597fb2d
549a2cc
 
a1551a6
 
 
5c8a315
a1551a6
 
 
0188ddf
5c8a315
 
 
 
b7d0cb0
 
 
 
 
 
b10dbaa
5c8a315
01defef
b10dbaa
01defef
1df563c
b10dbaa
5c8a315
b10dbaa
a1551a6
5c8a315
a472331
01defef
5c8a315
 
 
 
b673702
5c8a315
 
639a871
99b0a9f
5c8a315
 
8a8cff3
597fb2d
e5a285a
 
 
 
 
 
 
 
0188ddf
 
 
 
 
 
 
 
e5a285a
0188ddf
e5a285a
a1551a6
 
927c3de
a1551a6
 
 
 
 
 
927c3de
b10dbaa
5c8a315
597fb2d
 
 
 
 
 
 
 
6d4d03f
597fb2d
639a871
 
597fb2d
e5a285a
597fb2d
e5a285a
597fb2d
639a871
ebc2ece
a1551a6
 
 
 
24d64b3
a1551a6
 
 
0188ddf
99b0a9f
f9eef39
 
597fb2d
639a871
597fb2d
 
b10dbaa
e5a285a
 
 
 
 
 
6dcd218
 
 
 
 
a1551a6
e5a285a
a1551a6
 
 
e5a285a
5fe0186
b673702
 
5fe0186
 
 
 
 
 
 
b7d0cb0
5fe0186
b38a158
5fe0186
 
b673702
5fe0186
 
 
b7d0cb0
5fe0186
 
 
b38a158
5fe0186
b673702
549a2cc
b673702
597fb2d
01defef
49d14ca
3b1c44f
 
597fb2d
3b1c44f
49d14ca
 
5c8a315
49d14ca
 
b10dbaa
 
5c8a315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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()