Repository navigation
Expand file tree
/
Copy pathmap.py
More file actions
166 lines (137 loc) · 4.72 KB
/
Copy pathmap.py
File metadata and controls
166 lines (137 loc) · 4.72 KB
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
import streamlit as st
import pytz
import pandas as pd
from utils.pages import set_page_config
from utils.sidebar import show_sidebar_options, show_map_sidebar_code_examples
from utils.metrics import show_metrics
from utils.predicthq import (
get_api_key,
fetch_events,
ATTENDED_CATEGORIES,
NON_ATTENDED_CATEGORIES,
UNSCHEDULED_CATEGORIES,
)
from utils.map import show_map
def main():
set_page_config("Map")
show_sidebar_options()
if get_api_key() is not None:
map()
else:
st.warning("Please set a PredictHQ API Token.", icon="⚠️")
def map():
location = st.session_state.location if "location" in st.session_state else None
daterange = st.session_state.daterange if "daterange" in st.session_state else None
suggested_radius = (
st.session_state.suggested_radius
if "suggested_radius" in st.session_state
else None
)
radius = st.session_state.radius if "radius" in st.session_state else None
if (
location is None
or daterange is None
or suggested_radius is None
or radius is None
):
return
col1, col2 = st.columns([1,8])
with col1:
st.markdown("<img src='app/static/store-logo.png' width='160' />", unsafe_allow_html=True)
with col2:
st.header(location["address"])
show_metrics()
# Pull out date range to make them easier to work with
date_from = daterange["date_from"]
date_to = daterange["date_to"]
# Allow selecting categories
categories = ATTENDED_CATEGORIES + NON_ATTENDED_CATEGORIES + UNSCHEDULED_CATEGORIES
default_categories = ATTENDED_CATEGORIES
selected_categories = st.sidebar.multiselect(
"Event categories",
options=categories,
default=default_categories,
help="[Event Categories Docs](https://docs.predicthq.com/resources/events)",
)
# We have a bunch of code examples in the docs/code_examples folder
show_map_sidebar_code_examples()
# Fetch events
events = fetch_events(
location["lat"],
location["lon"],
radius=radius,
date_from=date_from,
date_to=date_to,
tz=location["tz"],
categories=selected_categories,
radius_unit=suggested_radius["radius_unit"],
)
# Show map and convert radius miles to meters (the map only supports meters)
show_map(
lat=location["lat"],
lon=location["lon"],
radius_meters=calc_meters(radius, suggested_radius["radius_unit"]),
events=events,
)
show_events_list(events, f'events-{location["id"]}-{date_from}-to-{date_to}')
def calc_meters(value, unit):
if unit == "mi":
return value * 1609
if unit == "ft":
return value * 0.3048
elif unit == "km":
return value * 1000
else:
return value
def show_events_list(events, filename="events"):
"""
We're also converting start/end times to local timezone here from UTC.
"""
results = []
for event in events["results"]:
venue = next(
filter(lambda entity: entity["type"] == "venue", event["entities"]), None
)
row = {
"id": event["id"],
"title": event["title"],
"phq_attendance": event["phq_attendance"] if event["phq_attendance"] else 0,
"category": event["category"],
"start_date_local": event["start"]
.astimezone(pytz.timezone(event["timezone"]))
.isoformat()
if event["timezone"] is not None
else event["start"].isoformat(),
"end_date_local": event["end"]
.astimezone(pytz.timezone(event["timezone"]))
.isoformat()
if event["timezone"] is not None
else event["end"].isoformat(),
"predicted_end_date_local": event["predicted_end"]
.astimezone(pytz.timezone(event["timezone"]))
.isoformat()
if "predicted_end" in event
and event["predicted_end"] is not None
and event["timezone"] is not None
else "",
"venue_name": venue["name"] if venue else "",
"venue_address": venue["formatted_address"] if venue else "",
"placekey": event["geo"]["placekey"]
if "geo" in event and "placekey" in event["geo"]
else "",
}
results.append(row)
events_df = pd.DataFrame(results)
st.dataframe(events_df)
@st.cache_data
def convert_df(df):
return df.to_csv().encode("utf-8")
csv = convert_df(events_df)
st.download_button(
label="✅ Download events as CSV",
data=csv,
file_name=f"{filename}.csv",
mime="text/csv",
)
if __name__ == "__main__":
main()