sandrachdi/sample_streamlit_app
0
1# -*- coding: utf-8 -*-2# Copyright 2018-2019 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 at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.15 16"""An example of showing geographic data."""17 18import streamlit as st19import pandas as pd20import numpy as np21import altair as alt22import pydeck as pdk23 24# SETTING PAGE CONFIG TO WIDE MODE25st.set_page_config(layout="wide")26 27# LOADING DATA28DATE_TIME = "date/time"29DATA_URL = (30 "http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz"31)32 33@st.cache(persist=True)34def load_data(nrows):35 data = pd.read_csv(DATA_URL, nrows=nrows)36 lowercase = lambda x: str(x).lower()37 data.rename(lowercase, axis="columns", inplace=True)38 data[DATE_TIME] = pd.to_datetime(data[DATE_TIME])39 return data40 41data = load_data(100000)42 43# CREATING FUNCTION FOR MAPS44 45def map(data, lat, lon, zoom):46 st.write(pdk.Deck(47 map_style="mapbox://styles/mapbox/light-v9",48 initial_view_state={49 "latitude": lat,50 "longitude": lon,51 "zoom": zoom,52 "pitch": 50,53 },54 layers=[55 pdk.Layer(56 "HexagonLayer",57 data=data,58 get_position=["lon", "lat"],59 radius=100,60 elevation_scale=4,61 elevation_range=[0, 1000],62 pickable=True,63 extruded=True,64 ),65 ]66 ))67 68# LAYING OUT THE TOP SECTION OF THE APP69row1_1, row1_2 = st.columns((2,3))70 71with row1_1:72 st.title("NYC Uber Ridesharing Data")73 hour_selected = st.slider("Select hour of pickup", 0, 23)74 75with row1_2:76 st.write(77 """78 ##79 Examining how Uber pickups vary over time in New York City's and at its major regional airports.80 By sliding the slider on the left you can view different slices of time and explore different transportation trends.81 """)82 83# FILTERING DATA BY HOUR SELECTED84data = data[data[DATE_TIME].dt.hour == hour_selected]85 86# LAYING OUT THE MIDDLE SECTION OF THE APP WITH THE MAPS87row2_1, row2_2, row2_3, row2_4 = st.columns((2,1,1,1))88 89# SETTING THE ZOOM LOCATIONS FOR THE AIRPORTS90la_guardia= [40.7900, -73.8700]91jfk = [40.6650, -73.7821]92newark = [40.7090, -74.1805]93zoom_level = 1294midpoint = (np.average(data["lat"]), np.average(data["lon"]))95 96with row2_1:97 st.write("**All New York City from %i:00 and %i:00**" % (hour_selected, (hour_selected + 1) % 24))98 map(data, midpoint[0], midpoint[1], 11)99 100with row2_2:101 st.write("**La Guardia Airport**")102 map(data, la_guardia[0],la_guardia[1], zoom_level)103 104with row2_3:105 st.write("**JFK Airport**")106 map(data, jfk[0],jfk[1], zoom_level)107 108with row2_4:109 st.write("**Newark Airport**")110 map(data, newark[0],newark[1], zoom_level)111 112# FILTERING DATA FOR THE HISTOGRAM113filtered = data[114 (data[DATE_TIME].dt.hour >= hour_selected) & (data[DATE_TIME].dt.hour < (hour_selected + 1))115 ]116 117hist = np.histogram(filtered[DATE_TIME].dt.minute, bins=60, range=(0, 60))[0]118 119chart_data = pd.DataFrame({"minute": range(60), "pickups": hist})120 121# LAYING OUT THE HISTOGRAM SECTION122 123st.write("")124 125st.write("**Breakdown of rides per minute between %i:00 and %i:00**" % (hour_selected, (hour_selected + 1) % 24))126 127st.altair_chart(alt.Chart(chart_data)128 .mark_area(129 interpolate='step-after',130 ).encode(131 x=alt.X("minute:Q", scale=alt.Scale(nice=False)),132 y=alt.Y("pickups:Q"),133 tooltip=['minute', 'pickups']134 ).configure_mark(135 opacity=0.2,136 color='red'137 ), use_container_width=True)138 