Dash / app.py
andrewammann's picture
Update app.py
a936db6 verified
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)