Spaces:
Runtime error
Runtime error
alpergel
commited on
Commit
•
f12c7fd
1
Parent(s):
eb1e1ee
push map demo
Browse files- app.py +194 -0
- requirements.txt +3 -0
- uber-raw-data-sep14.csv.gz +3 -0
app.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
# Copyright 2018-2022 Streamlit Inc.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
"""An example of showing geographic data."""
|
17 |
+
|
18 |
+
import os
|
19 |
+
|
20 |
+
import altair as alt
|
21 |
+
import numpy as np
|
22 |
+
import pandas as pd
|
23 |
+
import pydeck as pdk
|
24 |
+
import streamlit as st
|
25 |
+
|
26 |
+
# SETTING PAGE CONFIG TO WIDE MODE AND ADDING A TITLE AND FAVICON
|
27 |
+
st.set_page_config(layout="wide", page_title="NYC Ridesharing Demo", page_icon=":taxi:")
|
28 |
+
|
29 |
+
|
30 |
+
# LOAD DATA ONCE
|
31 |
+
@st.cache_resource
|
32 |
+
def load_data():
|
33 |
+
path = "uber-raw-data-sep14.csv.gz"
|
34 |
+
if not os.path.isfile(path):
|
35 |
+
path = f"https://github.com/streamlit/demo-uber-nyc-pickups/raw/main/{path}"
|
36 |
+
|
37 |
+
data = pd.read_csv(
|
38 |
+
path,
|
39 |
+
nrows=100000, # approx. 10% of data
|
40 |
+
names=[
|
41 |
+
"date/time",
|
42 |
+
"lat",
|
43 |
+
"lon",
|
44 |
+
], # specify names directly since they don't change
|
45 |
+
skiprows=1, # don't read header since names specified directly
|
46 |
+
usecols=[0, 1, 2], # doesn't load last column, constant value "B02512"
|
47 |
+
parse_dates=[
|
48 |
+
"date/time"
|
49 |
+
], # set as datetime instead of converting after the fact
|
50 |
+
)
|
51 |
+
|
52 |
+
return data
|
53 |
+
|
54 |
+
|
55 |
+
# FUNCTION FOR AIRPORT MAPS
|
56 |
+
def map(data, lat, lon, zoom):
|
57 |
+
st.write(
|
58 |
+
pdk.Deck(
|
59 |
+
map_style="mapbox://styles/mapbox/light-v9",
|
60 |
+
initial_view_state={
|
61 |
+
"latitude": lat,
|
62 |
+
"longitude": lon,
|
63 |
+
"zoom": zoom,
|
64 |
+
"pitch": 50,
|
65 |
+
},
|
66 |
+
layers=[
|
67 |
+
pdk.Layer(
|
68 |
+
"HexagonLayer",
|
69 |
+
data=data,
|
70 |
+
get_position=["lon", "lat"],
|
71 |
+
radius=100,
|
72 |
+
elevation_scale=4,
|
73 |
+
elevation_range=[0, 1000],
|
74 |
+
pickable=True,
|
75 |
+
extruded=True,
|
76 |
+
),
|
77 |
+
],
|
78 |
+
)
|
79 |
+
)
|
80 |
+
|
81 |
+
|
82 |
+
# FILTER DATA FOR A SPECIFIC HOUR, CACHE
|
83 |
+
@st.cache_data
|
84 |
+
def filterdata(df, hour_selected):
|
85 |
+
return df[df["date/time"].dt.hour == hour_selected]
|
86 |
+
|
87 |
+
|
88 |
+
# CALCULATE MIDPOINT FOR GIVEN SET OF DATA
|
89 |
+
@st.cache_data
|
90 |
+
def mpoint(lat, lon):
|
91 |
+
return (np.average(lat), np.average(lon))
|
92 |
+
|
93 |
+
|
94 |
+
# FILTER DATA BY HOUR
|
95 |
+
@st.cache_data
|
96 |
+
def histdata(df, hr):
|
97 |
+
filtered = data[
|
98 |
+
(df["date/time"].dt.hour >= hr) & (df["date/time"].dt.hour < (hr + 1))
|
99 |
+
]
|
100 |
+
|
101 |
+
hist = np.histogram(filtered["date/time"].dt.minute, bins=60, range=(0, 60))[0]
|
102 |
+
|
103 |
+
return pd.DataFrame({"minute": range(60), "pickups": hist})
|
104 |
+
|
105 |
+
|
106 |
+
# STREAMLIT APP LAYOUT
|
107 |
+
data = load_data()
|
108 |
+
|
109 |
+
# LAYING OUT THE TOP SECTION OF THE APP
|
110 |
+
row1_1, row1_2 = st.columns((2, 3))
|
111 |
+
|
112 |
+
# SEE IF THERE'S A QUERY PARAM IN THE URL (e.g. ?pickup_hour=2)
|
113 |
+
# THIS ALLOWS YOU TO PASS A STATEFUL URL TO SOMEONE WITH A SPECIFIC HOUR SELECTED,
|
114 |
+
# E.G. https://share.streamlit.io/streamlit/demo-uber-nyc-pickups/main?pickup_hour=2
|
115 |
+
if not st.session_state.get("url_synced", False):
|
116 |
+
try:
|
117 |
+
pickup_hour = int(st.experimental_get_query_params()["pickup_hour"][0])
|
118 |
+
st.session_state["pickup_hour"] = pickup_hour
|
119 |
+
st.session_state["url_synced"] = True
|
120 |
+
except KeyError:
|
121 |
+
pass
|
122 |
+
|
123 |
+
|
124 |
+
# IF THE SLIDER CHANGES, UPDATE THE QUERY PARAM
|
125 |
+
def update_query_params():
|
126 |
+
hour_selected = st.session_state["pickup_hour"]
|
127 |
+
st.experimental_set_query_params(pickup_hour=hour_selected)
|
128 |
+
|
129 |
+
|
130 |
+
with row1_1:
|
131 |
+
st.title("NYC Uber Ridesharing Data")
|
132 |
+
hour_selected = st.slider(
|
133 |
+
"Select hour of pickup", 0, 23, key="pickup_hour", on_change=update_query_params
|
134 |
+
)
|
135 |
+
|
136 |
+
|
137 |
+
with row1_2:
|
138 |
+
st.write(
|
139 |
+
"""
|
140 |
+
##
|
141 |
+
Examining how Uber pickups vary over time in New York City's and at its major regional airports.
|
142 |
+
By sliding the slider on the left you can view different slices of time and explore different transportation trends.
|
143 |
+
"""
|
144 |
+
)
|
145 |
+
|
146 |
+
# LAYING OUT THE MIDDLE SECTION OF THE APP WITH THE MAPS
|
147 |
+
row2_1, row2_2, row2_3, row2_4 = st.columns((2, 1, 1, 1))
|
148 |
+
|
149 |
+
# SETTING THE ZOOM LOCATIONS FOR THE AIRPORTS
|
150 |
+
la_guardia = [40.7900, -73.8700]
|
151 |
+
jfk = [40.6650, -73.7821]
|
152 |
+
newark = [40.7090, -74.1805]
|
153 |
+
zoom_level = 12
|
154 |
+
midpoint = mpoint(data["lat"], data["lon"])
|
155 |
+
|
156 |
+
with row2_1:
|
157 |
+
st.write(
|
158 |
+
f"""**All New York City from {hour_selected}:00 and {(hour_selected + 1) % 24}:00**"""
|
159 |
+
)
|
160 |
+
map(filterdata(data, hour_selected), midpoint[0], midpoint[1], 11)
|
161 |
+
|
162 |
+
with row2_2:
|
163 |
+
st.write("**La Guardia Airport**")
|
164 |
+
map(filterdata(data, hour_selected), la_guardia[0], la_guardia[1], zoom_level)
|
165 |
+
|
166 |
+
with row2_3:
|
167 |
+
st.write("**JFK Airport**")
|
168 |
+
map(filterdata(data, hour_selected), jfk[0], jfk[1], zoom_level)
|
169 |
+
|
170 |
+
with row2_4:
|
171 |
+
st.write("**Newark Airport**")
|
172 |
+
map(filterdata(data, hour_selected), newark[0], newark[1], zoom_level)
|
173 |
+
|
174 |
+
# CALCULATING DATA FOR THE HISTOGRAM
|
175 |
+
chart_data = histdata(data, hour_selected)
|
176 |
+
|
177 |
+
# LAYING OUT THE HISTOGRAM SECTION
|
178 |
+
st.write(
|
179 |
+
f"""**Breakdown of rides per minute between {hour_selected}:00 and {(hour_selected + 1) % 24}:00**"""
|
180 |
+
)
|
181 |
+
|
182 |
+
st.altair_chart(
|
183 |
+
alt.Chart(chart_data)
|
184 |
+
.mark_area(
|
185 |
+
interpolate="step-after",
|
186 |
+
)
|
187 |
+
.encode(
|
188 |
+
x=alt.X("minute:Q", scale=alt.Scale(nice=False)),
|
189 |
+
y=alt.Y("pickups:Q"),
|
190 |
+
tooltip=["minute", "pickups"],
|
191 |
+
)
|
192 |
+
.configure_mark(opacity=0.2, color="red"),
|
193 |
+
use_container_width=True,
|
194 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.20.0
|
2 |
+
pydeck==0.7.1
|
3 |
+
protobuf==3.19.5
|
uber-raw-data-sep14.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:07b4353d7b76c14b5893080d4c5e602066510a32abbdfabea283a7588c2a2d8c
|
3 |
+
size 6834535
|