Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,49 +2,35 @@ from dash import Dash, html, dcc, Input, Output
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
5 |
-
# Load the
|
6 |
-
df = pd.read_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(
|
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='
|
26 |
-
options=[{'label':
|
27 |
-
value=
|
28 |
),
|
29 |
-
|
30 |
-
dcc.Graph(
|
31 |
-
id='example-graph',
|
32 |
-
figure=fig
|
33 |
-
)
|
34 |
])
|
35 |
|
36 |
-
#
|
37 |
@app.callback(
|
38 |
-
Output('
|
39 |
-
[Input('
|
40 |
)
|
41 |
-
def
|
42 |
-
filtered_df =
|
43 |
-
fig = px.bar(filtered_df, x='
|
44 |
return fig
|
45 |
|
46 |
# Run the app
|
47 |
if __name__ == '__main__':
|
48 |
-
app.
|
|
|
49 |
|
50 |
|
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
5 |
+
# Load the CSV file
|
6 |
+
df = pd.read_csv('cincinnati_traffic_crash_data__cpd.csv')
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Initialize the Dash app
|
9 |
app = Dash(__name__)
|
10 |
|
|
|
|
|
|
|
11 |
# Define the layout of the app
|
12 |
+
app.layout = html.Div([
|
|
|
|
|
|
|
|
|
13 |
dcc.Dropdown(
|
14 |
+
id='city-dropdown',
|
15 |
+
options=[{'label': city, 'value': city} for city in df['City'].unique()],
|
16 |
+
value='SF'
|
17 |
),
|
18 |
+
dcc.Graph(id='bar-chart')
|
|
|
|
|
|
|
|
|
19 |
])
|
20 |
|
21 |
+
# Define the callback to update the graph
|
22 |
@app.callback(
|
23 |
+
Output('bar-chart', 'figure'),
|
24 |
+
[Input('city-dropdown', 'value')]
|
25 |
)
|
26 |
+
def update_chart(selected_city):
|
27 |
+
filtered_df = df[df['City'] == selected_city]
|
28 |
+
fig = px.bar(filtered_df, x='Fruit', y='Amount', color='Fruit', barmode='group')
|
29 |
return fig
|
30 |
|
31 |
# Run the app
|
32 |
if __name__ == '__main__':
|
33 |
+
app.run_server(debug=True, host='0.0.0.0', port=7860)
|
34 |
+
|
35 |
|
36 |
|