andrewammann commited on
Commit
769c419
·
verified ·
1 Parent(s): a5a0fdb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dash import Dash, html, dcc, Input, Output
2
+ import pandas as pd
3
+ import plotly.express as px
4
+
5
+ # Load the dataset
6
+ df = pd.read_csv('C:/Users/aammann/OneDrive - Great American Insurance Group/Documents/cincinnati_traffic_crash_data__cpd.csv')
7
+
8
+ # Preprocess the data
9
+ df_grouped = df.groupby('SNA_NEIGHBORHOOD')['DATECRASHREPORTED'].count().reset_index()
10
+ df_grouped.columns = ['Neighborhood', 'Total Crashes']
11
+
12
+ # Initialize the Dash app
13
+ app = Dash(__name__)
14
+
15
+ # Create a bar chart
16
+ fig = px.bar(df_grouped, x='Neighborhood', y='Total Crashes', title='Total Traffic Crashes by Neighborhood in Cincinnati')
17
+
18
+ # Define the layout of the app
19
+ app.layout = html.Div(children=[
20
+ html.H1(children='Cincinnati Traffic Crashes Dashboard'),
21
+
22
+ html.Div(children='''Dash: A web application framework for your data.'''),
23
+
24
+ dcc.Dropdown(
25
+ id='neighborhood-dropdown',
26
+ options=[{'label': n, 'value': n} for n in df_grouped['Neighborhood']],
27
+ value=df_grouped['Neighborhood'][0]
28
+ ),
29
+
30
+ dcc.Graph(
31
+ id='example-graph',
32
+ figure=fig
33
+ )
34
+ ])
35
+
36
+ # Callback to update graph based on dropdown selection
37
+ @app.callback(
38
+ Output('example-graph', 'figure'),
39
+ [Input('neighborhood-dropdown', 'value')]
40
+ )
41
+ def update_graph(selected_neighborhood):
42
+ filtered_df = df_grouped[df_grouped['Neighborhood'] == selected_neighborhood]
43
+ fig = px.bar(filtered_df, x='Neighborhood', y='Total Crashes', title=f'Total Traffic Crashes in {selected_neighborhood}')
44
+ return fig
45
+
46
+ # Run the app
47
+ if __name__ == '__main__':
48
+ app.run(debug=True, port=8051)
49
+
50
+