-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_app.py
199 lines (168 loc) · 7.6 KB
/
my_app.py
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
194
195
196
197
198
199
import streamlit as st
import pydeck as pdk
import pandas as pd
import logging
import os.path
import re
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# from geopy.geocoders import Nominatim
# geolocator = Nominatim(user_agent="arsenalamerica-branches")
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
sheet_url = st.secrets["gsheets"]["private_gsheets_url"]
branches_sheet = 'FormResponses!A:Y'
coordinates_sheet = 'Coordinates!A:E'
def create_connection():
credentials = service_account.Credentials.from_service_account_info(
st.secrets["gcp_service_account"],
scopes=["https://www.googleapis.com/auth/spreadsheets",],)
return credentials
def write_data():
...
@st.cache_data(ttl=3600)
def get_data():
creds = create_connection()
try:
service = build('sheets', 'v4', credentials=creds, cache_discovery=False)
# Call the Sheets API
sheet = service.spreadsheets()
branches = sheet.values().get(spreadsheetId="1aMm4dHHmzA2Xc8uWnpUaU_5xVjjAIGHltn7jXl-OIjc",
range=branches_sheet).execute()
branches_data = branches.get('values', [])
coordinates = sheet.values().get(spreadsheetId="1aMm4dHHmzA2Xc8uWnpUaU_5xVjjAIGHltn7jXl-OIjc",
range=coordinates_sheet).execute()
coordinates_data = coordinates.get('values', [])
if not branches_data or not coordinates_data:
print('No data found.')
return
branches_df = pd.DataFrame(data=branches_data[1:], columns=branches_data[0])
coordinates_df = pd.DataFrame(data=coordinates_data[1:], columns=coordinates_data[0])
df = branches_df.merge(coordinates_df, how='inner', left_on=['Branch Name', 'Pub Name'], right_on=['Branch Name', 'Pub Name'])
df = df.sort_values('Timestamp').groupby(['Branch Name', 'Pub Name']).tail(1).reset_index(drop=True)
df = df.sort_values(['Pub State', 'Pub City']).reset_index(drop=True)
return df
except HttpError as err:
print(err)
def main():
df = get_data()
df = df.rename(columns={'Latitude': 'latitude', 'Longitude': 'longitude'})
df = df.astype({'latitude':'float','longitude':'float'})
st.title('Arsenal America')
row1_col1, row1_col2 = st.columns([.25,.25])
state_options = list(set(df['Pub State']))
state_options.sort()
with row1_col1:
STATE_SELECT = st.selectbox('If there is a branch in your state you can search for it below:',
["Please select"] + state_options,
key='state_select'
)
with row1_col2:
def reset():
st.session_state.state_select = 'Please select'
st.text('')
st.text('')
st.button('Reset', on_click=reset)
# filter data based on selections
all_states = STATE_SELECT == 'Please select'
if not all_states:
df = df[df['Pub State'] == STATE_SELECT].reset_index()
icon_data = {
"url": "https://image-service.onefootball.com/transform?w=128&dpr=2&image=https://images.onefootball.com/icons/teams/164/2.png",
"width": 64,
"height": 64,
"anchorY": 64
}
tooltip = {
"html": "<b>Branch:</b> {Branch Name} <br/> <b>Pub:</b> {Pub Name}",
"style": {
"backgroundColor": "gray",
"color": "white"
}
}
df['icon_data'] = pd.Series([icon_data for x in range(len(df.index))])
icon_layer = pdk.Layer(
type="IconLayer",
data=df,
get_icon="icon_data",
get_size=4,
size_scale=15,
get_position=["longitude", "latitude"],
pickable=True
)
if all_states:
view_state = pdk.ViewState(longitude=-98.5,latitude=38.500, zoom=3)
# view_state = pdk.data_utils.compute_view(df[["longitude", "latitude"]], 1)
else:
view_state = pdk.ViewState(
longitude=(
max(df['longitude']) - (
max(df['longitude']) -
min(df['longitude'])
)/2),
latitude=max(df['latitude']), zoom=5
)
r = pdk.Deck(
map_style=None,
layers=[icon_layer],
initial_view_state=view_state,
tooltip=tooltip
)
st.pydeck_chart(r)
states_df = df.groupby(by='Pub State')
states = dict(list(states_df))
for state in states:
state_pubs_df = states_df.get_group(state)
state_pubs_df = state_pubs_df.sort_values(by=['Branch Name', 'Pub City'])
pubs = list(state_pubs_df['Pub Name'].tolist())
pub_groups = zip(*(iter(pubs),) * 2) if len(pubs)>1 else [tuple(pubs)]
with st.expander(state, not all_states):
for group in pub_groups:
col1, col2 = st.columns([1,1])
cols = [col1, col2]
for count, col in enumerate(cols):
pub = group[count] if len(group) >= count+1 else None
if pub:
with col:
pub_data = state_pubs_df[state_pubs_df['Pub Name']==pub].to_dict('records')[0]
st.subheader(pub_data['Branch Name'])
st.caption(pub)
pub_col1, pub_col2, pub_col3, _ = st.columns([1,1,1,5])
with pub_col1:
link_nav = re.sub(' ', '%20',
'https://www.google.com/maps/search/' + '+'.join([
pub_data.get('Pub Name'),
pub_data.get('Pub Address 1'),
pub_data.get('Pub City'),
pub_data.get('Pub State'),
pub_data.get('Pub ZIP Code'),
])
)
st.markdown(
f'[<img src="./app/static/gmaps.png" height="21">]({link_nav})',
unsafe_allow_html=True,
)
st.text('')
with pub_col2:
link_fb = pub_data.get('Branch Facebook Page')
if link_fb:
link_fb = re.sub(r'^.*?facebook', 'https://facebook', link_fb)
st.markdown(
f'[<img src="./app/static/fb.png" height="21">]({link_fb})',
unsafe_allow_html=True,
)
st.text('')
with pub_col3:
link_twitter = pub_data.get('Branch Twitter Handle')
if link_twitter:
link_twitter = f'https://twitter.com/{link_twitter}'
st.markdown(
f'[<img src="./app/static/twitter.png" height="21">]({link_twitter})',
unsafe_allow_html=True,
)
st.text('')
# with pub_col2:
# st.markdown("![Foo](http://www.google.com.au/images/nav_logo7.png)(http://google.com.au/)")
# st.image(image=img_nav, width=14)
if __name__ == '__main__':
main()