File size: 1,777 Bytes
b10dbaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 not "sleep_time" in st.session_state:
    st.session_state.sleep_time = 2

if not "auto_refresh" in st.session_state:
    st.session_state.auto_refresh = True

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("Streamlit Map Dashboard")
st.subheader("Map with Folium in Streamlit")

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")

m = folium.Map(location=[31.628674, -7.992047], zoom_start=7.5)

for index, row in df.iterrows():
    if row['latlng'] is None:
        continue
    folium.Marker(
        location=row['latlng'],
        tooltip=row['العنوان أو اسم القرية'] if not pd.isna(row['العنوان أو اسم القرية']) else None,
        popup=row['رقم الهاتف (اختياري)'] if not pd.isna(row['رقم الهاتف (اختياري)']) else None,
    ).add_to(m)

st_data = st_folium(m, use_container_width=True)

if auto_refresh:
    time.sleep(number)
    st.experimental_rerun()