-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuber_plus.py
More file actions
133 lines (107 loc) · 4.12 KB
/
uber_plus.py
File metadata and controls
133 lines (107 loc) · 4.12 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
# -*- coding: utf-8 -*-
# Copyright 2018-2019 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An example of showing geographic data."""
import streamlit as st
import pandas as pd
import numpy as np
import altair as alt
import pydeck as pdk
DATE_TIME = "date/time"
DATA_URL = (
"http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz"
)
# setting up the layout for the page
analysis = st.sidebar.radio("Choose analysis:", ("All Pickups", "Airport Pickups"))
@st.cache(persist=True)
def load_data(nrows):
data = pd.read_csv(DATA_URL, nrows=nrows)
lowercase = lambda x: str(x).lower()
data.rename(lowercase, axis="columns", inplace=True)
data[DATE_TIME] = pd.to_datetime(data[DATE_TIME])
return data
data = load_data(100000)
def map(lat, lon, zoom):
st.write(pdk.Deck(
map_style="mapbox://styles/mapbox/light-v9",
initial_view_state={
"latitude": lat,
"longitude": lon,
"zoom": zoom,
"pitch": 50,
},
layers=[
pdk.Layer(
"HexagonLayer",
data=data,
get_position=["lon", "lat"],
radius=100,
elevation_scale=4,
elevation_range=[0, 1000],
pickable=True,
extruded=True,
),
],
))
if analysis == "All Pickups":
st.title("Uber Pickups in New York City")
st.markdown(
"""
This is a demo of a Streamlit app that shows the Uber pickups
geographical distribution in New York City. Use the slider
to pick a specific hour and look at how the charts change.
[See source code](https://github.com/streamlit/demo-uber-nyc-pickups/blob/master/app.py)
""")
hour = st.slider("Hour of pickup", 0, 23)
data = data[data[DATE_TIME].dt.hour == hour]
st.subheader("Geo data between %i:00 and %i:00" % (hour, (hour + 1) % 24))
midpoint = (np.average(data["lat"]), np.average(data["lon"]))
map(midpoint[0], midpoint[1], 11)
st.subheader("Breakdown by minute between %i:00 and %i:00" % (hour, (hour + 1) % 24))
filtered = data[
(data[DATE_TIME].dt.hour >= hour) & (data[DATE_TIME].dt.hour < (hour + 1))
]
hist = np.histogram(filtered[DATE_TIME].dt.minute, bins=60, range=(0, 60))[0]
chart_data = pd.DataFrame({"minute": range(60), "pickups": hist})
st.altair_chart(alt.Chart(chart_data)
.mark_area(
interpolate='step-after',
).encode(
x=alt.X("minute:Q", scale=alt.Scale(nice=False)),
y=alt.Y("pickups:Q"),
tooltip=['minute', 'pickups']
), use_container_width=True)
if st.checkbox("Show raw data", False):
st.subheader("Raw data by minute between %i:00 and %i:00" % (hour, (hour + 1) % 24))
st.write(data)
if analysis == "Airport Pickups":
st.title("Uber Airport Pickups")
st.write("Examining how Uber pickups vary over time at New York City's three major airports.")
hour_airport = st.slider("Hour of airport pickup", 0, 23)
data = data[data[DATE_TIME].dt.hour == hour_airport]
st.subheader("Airport pickups between %i:00 and %i:00" % (hour_airport, (hour_airport + 1) % 24))
c1, c2, c3 = st.columns(3)
la_guardia= [40.7900, -73.8700]
jfk = [40.6650, -73.7821]
newark = [40.7090, -74.1805]
zoom_level = 12
with c1:
st.write("**La Guardia Airport**")
map(la_guardia[0],la_guardia[1], zoom_level)
with c2:
st.write("**JFK Airport**")
map(jfk[0],jfk[1], zoom_level)
with c3:
st.write("**Newark Airport**")
map(newark[0],newark[1], zoom_level)