alpergel commited on
Commit
323a18a
1 Parent(s): 0cf9c0e
Files changed (2) hide show
  1. app.py +15 -129
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,73 +1,36 @@
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 = "ipc.csv"
34
-
35
-
36
  data = pd.read_csv(
37
  path,
38
- nrows=100000, # approx. 10% of data
39
- names=[
40
- "Lat",
41
- "Lon",
42
- "Number",
43
- "Tract",
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/dark-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],
@@ -75,90 +38,13 @@ def map(data, lat, lon, zoom):
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
-
163
- # CALCULATING DATA FOR THE HISTOGRAM
164
- #chart_data = histdata(data, hour_selected)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ # Streamlined version of the Streamlit app code
3
 
 
 
 
 
4
  import pandas as pd
5
  import pydeck as pdk
6
  import streamlit as st
7
 
8
  # SETTING PAGE CONFIG TO WIDE MODE AND ADDING A TITLE AND FAVICON
9
+ st.set_page_config(layout="wide", page_title="Data Visualization", page_icon=":chart_with_upwards_trend:")
 
10
 
11
+ # Function to load data
 
12
  def load_data():
13
  path = "ipc.csv"
 
 
14
  data = pd.read_csv(
15
  path,
16
+ names=["Lat", "Lon", "Number", "Census Tract #"],
17
+ skiprows=1
 
 
 
 
 
 
 
 
 
 
18
  )
 
19
  return data
20
 
21
+ # Function to display the map
 
22
  def map(data, lat, lon, zoom):
23
+ tooltip = {"html": "<b>Census Tract #:</b> {Census Tract #}<br><b>Number:</b> {Number}"}
24
  st.write(
25
  pdk.Deck(
26
  map_style="mapbox://styles/mapbox/dark-v9",
27
+ initial_view_state={"latitude": lat, "longitude": lon, "zoom": zoom, "pitch": 50},
 
 
 
 
 
28
  layers=[
29
  pdk.Layer(
30
  "HexagonLayer",
31
  data=data,
32
+ get_position=["Lon", "Lat"],
33
+ get_elevation="Number",
34
  radius=100,
35
  elevation_scale=4,
36
  elevation_range=[0, 1000],
 
38
  extruded=True,
39
  ),
40
  ],
41
+ tooltip=tooltip
42
  )
43
  )
44
 
45
+ # Main app execution part
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  data = load_data()
47
+ midpoint = (data['Lat'].mean(), data['Lon'].mean())
48
 
49
+ st.title("Interactive Data Visualization")
50
+ map(data, midpoint[0], midpoint[1], 11)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  streamlit==1.20.0
2
  pydeck==0.7.1
3
- protobuf==3.19.5
 
 
1
  streamlit==1.20.0
2
  pydeck==0.7.1
3
+ protobuf==3.19.5
4
+ pandas