Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283843 unmodified linesimport pydeck as pdkimport plotly.express as pxst.title("Motor Vehicle Collision in New York Dashboard")st.markdown("Streamlit dashboard for motor vehicle colllision analysis in New York City 💥🏍️")@st.cache(persist = True)def load_data(n_rows):data = pd.read_csv("Motor_Vehicle_Collisions_-_Crashes.csv" , nrows = n_rows , parse_dates = [['CRASH DATE','CRASH TIME']])data.dropna(subset = ['LATITUDE','LONGITUDE'],inplace =True)lowercase = lambda x : str(x).lower()data.rename(lowercase , axis = 'columns', inplace = True)data.rename(columns = {'crash date_crash time':'date/time'},inplace=True)return datadata = load_data(100000)original_data = datacols = str(data.columns)st.header("Where are the most people injured in NYC?")injured_people = st.slider("Number of people injured in vehicle colllisions" , 0 ,19)st.map(data.query("injured_persons >= @injured_people")[['latitude','longitude']].dropna(how ='any'))st.header("How many collisions occur during a given time of the day?")hour = st.slider("Houe to look at " , 0,23,1)data = data[data['date/time'].dt.hour == hour]st.markdown("Vehical collisions between %i:00 and %i:00 "% (hour, (hour+1 ) % 24))midpoint = (np.average(data['latitude']), np.average(data['longitude']))st.write(pdk.Deck(map_style = "mapbox://styles/mapbox/light-v9",initial_view_state ={'latitude':midpoint[0],'longitude':midpoint[1],'zoom':11,'pitch':50,},layers =[pdk.Layer("HexagonLayer",data = data[['date/time', 'latitude','longitude']],get_position = ['longitude','latitude'],radius = 100,extruded = True,pickable = True,elevation_scale = 4 ,elevation_range = [0,1000],),]))st.subheader("Breakdowm by minute between %i:00 and %i:00 "%(hour,(hour+1)%24))filter = data[(data['date/time'].dt.hour >= hour) & (data['date/time'].dt.hour < (hour+1))]hist = np.histogram(filter['date/time'].dt.minute , bins = 60 , range = (0,60))[0]chart_data = pd.DataFrame({'minute':range(60) , 'crashes':hist})fig = px.bar(chart_data , x = 'minute' , y ='crashes' ,hover_data = ['minute','crashes'],height = 400)st.write(fig)st.header("Top 5 dangerous streets affected type")select = st.selectbox("Affected type of people", ['Pedestrians','Cyclists','Motorists'])if select == 'Pedestrians':st.write(original_data.query("injured_pedestrians >= 1")[["on street name","injured_pedestrians"]].sort_values(by = ['injured_pedestrians'], ascending = False).dropna(how='any')[:5])elif select == 'Cyclists':st.write(original_data.query("injured_cyclists >= 1")[["on street name","injured_cyclists"]].sort_values(by = ['injured_cyclists'], ascending = False).dropna(how='any')[:5])else:st.write(original_data.query("injured_motorists >= 1")[["on street name","injured_motorists"]].sort_values(by = ['injured_motorists'], ascending = False).dropna(how='any')[:5])if st.checkbox("Show Raw Data",False):st.subheader("Raw Data")st.write(data)3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091923 unmodified linesimport pydeck as pdkimport plotly.express as px# Create tabs in the sidebarst.sidebar.title("Navigation")tabs = st.sidebar.radio("Go to", ('Introduction', 'Injured People', 'Collisions by Time', 'Dangerous Streets', 'Raw Data'))# Load the data@st.cache(persist=True)def load_data(n_rows):data = pd.read_csv("Motor_Vehicle_Collisions_-_Crashes.csv", nrows=n_rows, parse_dates=[['CRASH DATE', 'CRASH TIME']])data.dropna(subset=['LATITUDE', 'LONGITUDE'], inplace=True)lowercase = lambda x: str(x).lower()data.rename(lowercase, axis='columns', inplace=True)data.rename(columns={'crash date_crash time': 'date/time'}, inplace=True)return datadata = load_data(100000)original_data = data# Introduction tabif tabs == 'Introduction':st.title("Motor Vehicle Collision in New York Dashboard")st.markdown("Streamlit dashboard for motor vehicle collision analysis in New York City 💥🏍️")# Code for the introduction section goes here# Injured People tabelif tabs == 'Injured People':st.title("Where are the most people injured in NYC?")injured_people = st.slider("Number of people injured in vehicle collisions", 0, 19)st.map(data.query("injured_persons >= @injured_people")[['LATITUDE', 'LONGITUDE']].dropna(how='any'))# Code for the injured people section goes here# Collisions by Time tabelif tabs == 'Collisions by Time':st.title("How many collisions occur during a given time of the day?")hour = st.slider("Hour to look at", 0, 23, 1)data = data[data['date/time'].dt.hour == hour]midpoint = (np.average(data['LATITUDE']), np.average(data['LONGITUDE']))st.write(pdk.Deck(map_style="mapbox://styles/mapbox/light-v9",initial_view_state={'latitude': midpoint[0],'longitude': midpoint[1],'zoom': 11,'pitch': 50,},layers=[pdk.Layer("HexagonLayer",data=data[['date/time', 'LATITUDE', 'LONGITUDE']],get_position=['LONGITUDE', 'LATITUDE'],radius=100,extruded=True,pickable=True,elevation_scale=4,elevation_range=[0, 1000],),]))filter = data[(data['date/time'].dt.hour >= hour) & (data['date/time'].dt.hour < (hour + 1))]hist = np.histogram(filter['date/time'].dt.minute, bins=60, range=(0, 60))[0]chart_data = pd.DataFrame({'minute': range(60), 'crashes': hist})fig = px.bar(chart_data, x='minute', y='crashes', hover_data=['minute', 'crashes'], height=400)st.write(fig)# Code for the collisions by time section goes here# Dangerous Streets tabelif tabs == 'Dangerous Streets':st.title("Top 5 dangerous streets affected by type")select = st.selectbox("Affected type of people", ['Pedestrians', 'Cyclists', 'Motorists'])if select == 'Pedestrians':st.write(original_data.query("injured_pedestrians >= 1")[["ON STREET NAME", "injured_pedestrians"]].sort_values(by=['injured_pedestrians'], ascending=False).dropna(how='any')[:5])elif select == 'Cyclists':st.write(original_data.query("injured_cyclists >= 1")[["ON STREET NAME", "injured_cyclists"]].sort_values(by=['injured_cyclists'], ascending=False).dropna(how='any')[:5])else:st.write(original_data.query("injured_motorists >= 1")[["ON STREET NAME", "injured_motorists"]].sort_values(by=['injured_motorists'], ascending=False).dropna(how='any')[:5])# Code for the dangerous streets section goes here# Raw Data tabelse:st.title("Raw Data")if st.checkbox("Show Raw Data", False):st.subheader("Raw Data")st.write(data)# Code for the raw data section goes here
app.py+79-71