Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import folium | |
import streamlit as st | |
import pandas as pd | |
from streamlit_folium import st_folium | |
import time | |
st.set_page_config(layout="wide") | |
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 | |
st.title("Nt3awnu kamlin نتعاونو كاملين") | |
st.markdown("Help us report more people in need by filling this form: https://forms.gle/nZNCUVog9ka2Vdqu6") | |
def parse_gg_sheet(url): | |
url = url.replace("edit#gid=", "export?format=csv&gid=") | |
df = pd.read_csv(url) | |
# parse latlng (column 4) to [lat, lng] | |
def parse_latlng(latlng): | |
if type(latlng) == str and "," in latlng: | |
lat, lng = latlng.split(",") | |
return [float(lat), float(lng)] | |
else: | |
return None | |
return df.assign(latlng=df.iloc[:, 4].apply(parse_latlng)) | |
df = parse_gg_sheet( | |
"https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/edit#gid=966953708" | |
) | |
headers_mapping = { | |
"إغاثة": "Rescue/إغاثة", | |
"مساعدة طبية": "Medical Assistance/مساعدة طبية", | |
"مأوى": "Shelter/مأوى", | |
"طعام وماء": "Food & Water/طعام وماء", | |
"مخاطر (تسرب الغاز، تلف في الخدمات العامة...)": "Danger/مخاطر", | |
} | |
colors_mapping = { | |
"إغاثة": "red", | |
"مساعدة طبية": "orange", | |
"مأوى": "yellow", | |
"طعام وماء": "blue", | |
"مخاطر (تسرب الغاز، تلف في الخدمات العامة...)": "grey", | |
} | |
options = ["إغاثة", "مساعدة طبية", "مأوى", "طعام وماء", "مخاطر (تسرب الغاز، تلف في الخدمات العامة...)"] | |
selected_options = [] | |
st.markdown("👉 **Choose request type / اختر نوع الطلب**") | |
cols = st.columns(len(options)) | |
for i, option in enumerate(options): | |
default_value = True if option == "إغاثة" else False | |
checked = cols[i].checkbox(option, value=default_value) | |
if checked: | |
selected_options.append(option) | |
df['id'] = df.index | |
filtered_df = df[df[' What are your needs? / ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)'].isin(selected_options)] | |
selected_headers = [headers_mapping[request] for request in selected_options] | |
st.subheader(", ".join(selected_headers)) | |
m = folium.Map( | |
location=[31.628674, -7.992047], | |
zoom_start=8.5, | |
min_zoom=8.5, | |
max_lat=35.628674, | |
min_lat=29.628674, | |
max_lon=-4.992047, | |
min_lon=-10.992047, | |
max_bounds=True, | |
) | |
for index, row in filtered_df.iterrows(): | |
request_type = row[' What are your needs? / ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)'] | |
phone = row["رقم الهاتف (اختياري)"] if not pd.isna(row["رقم الهاتف (اختياري)"]) else "" | |
display_text = f"{request_type} {phone}" | |
if row["latlng"] is None: | |
continue | |
folium.Marker( | |
location=row["latlng"], | |
tooltip=row[' لأي قيادة / دوار تنتمون ؟'] if not pd.isna(row[' لأي قيادة / دوار تنتمون ؟']) else None, | |
popup=display_text, | |
icon=folium.Icon(colors_mapping[request_type]) | |
).add_to(m) | |
st_data = st_folium(m, use_container_width=True) | |
# Google Sheet Table | |
st.markdown( | |
""" | |
<iframe src="https://docs.google.com/spreadsheets/d/1gYoBBiBo1L18IVakHkf3t1fOGvHWb23loadyFZUeHJs/" width="100%" height="600px"></iframe> | |
""", | |
unsafe_allow_html=True, | |
) | |
# Google Form | |
# st.markdown( | |
# """ | |
# <iframe src="https://docs.google.com/forms/d/e/1FAIpQLSfNrFHUScot5jQyRUJmor7kWYgwkkME6WqRgQG_L4Eze4RdHA/viewform?embedded=true" width="640" height="2877" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe> | |
# """, | |
# unsafe_allow_html=True, | |
# ) | |
if auto_refresh: | |
time.sleep(number) | |
st.experimental_rerun() | |