Spaces:
Sleeping
Sleeping
File size: 1,538 Bytes
769c419 9722248 97275a2 8cebeeb f7a34f7 97275a2 769c419 a936db6 769c419 97275a2 769c419 97275a2 769c419 97275a2 769c419 97275a2 769c419 97275a2 769c419 97275a2 769c419 97275a2 769c419 97275a2 f8a495e 9722248 769c419 |
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 |
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px
# # Load the dataset
df = pd.read_parquet('cincinnati_traffic_crash_data__cpd.parquet')
# Preprocess the data
df_grouped = df.groupby('SNA_NEIGHBORHOOD')['DATECRASHREPORTED'].count().reset_index()
df_grouped.columns = ['Neighborhood', 'Total Crashes']
# Initialize the Dash app
app = Dash(__name__)
server = app.server
# Create a bar chart
fig = px.bar(df_grouped, x='Neighborhood', y='Total Crashes', title='Total Traffic Crashes by Neighborhood in Cincinnati')
# Define the layout of the app
app.layout = html.Div(children=[
html.H1(children='Cincinnati Traffic Crashes Dashboard'),
html.Div(children='''Dash: A web application framework for your data.'''),
dcc.Dropdown(
id='neighborhood-dropdown',
options=[{'label': n, 'value': n} for n in df_grouped['Neighborhood']],
value=df_grouped['Neighborhood'][0]
),
dcc.Graph(
id='example-graph',
figure=fig
)
])
# Callback to update graph based on dropdown selection
@app.callback(
Output('example-graph', 'figure'),
[Input('neighborhood-dropdown', 'value')]
)
def update_graph(selected_neighborhood):
filtered_df = df_grouped[df_grouped['Neighborhood'] == selected_neighborhood]
fig = px.bar(filtered_df, x='Neighborhood', y='Total Crashes', title=f'Total Traffic Crashes in {selected_neighborhood}')
return fig
# Run the app
if __name__ == '__main__':
app.run(debug=True, port=8051)
|