Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,54 @@
|
|
| 1 |
from dash import Dash, html, dcc, Input, Output
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
| 4 |
-
import os
|
| 5 |
-
from kaggle.api.kaggle_api_extended import KaggleApi
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Initialize the Kaggle API
|
| 14 |
-
api = KaggleApi()
|
| 15 |
-
api.authenticate()
|
| 16 |
-
|
| 17 |
-
# Download the dataset
|
| 18 |
-
dataset_path = 'cincinnati-car-crash-data.csv'
|
| 19 |
-
if not os.path.exists(dataset_path):
|
| 20 |
-
api.dataset_download_file('steverusso/cincinnati-car-crash-data', 'cincinnati-car-crash-data.csv', path='.')
|
| 21 |
-
# Unzip the downloaded file if necessary
|
| 22 |
-
import zipfile
|
| 23 |
-
with zipfile.ZipFile('cincinnati-car-crash-data.csv.zip', 'r') as zip_ref:
|
| 24 |
-
zip_ref.extractall('.')
|
| 25 |
-
|
| 26 |
-
# Load the CSV file
|
| 27 |
-
df = pd.read_csv(dataset_path)
|
| 28 |
|
| 29 |
# Initialize the Dash app
|
| 30 |
app = Dash(__name__)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
# Define the layout of the app
|
| 33 |
-
app.layout = html.Div([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
dcc.Dropdown(
|
| 35 |
-
id='
|
| 36 |
-
options=[{'label':
|
| 37 |
-
value='
|
| 38 |
),
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
])
|
| 41 |
|
| 42 |
-
#
|
| 43 |
@app.callback(
|
| 44 |
-
Output('
|
| 45 |
-
[Input('
|
| 46 |
)
|
| 47 |
-
def
|
| 48 |
-
filtered_df =
|
| 49 |
-
fig = px.bar(filtered_df, x='
|
| 50 |
return fig
|
| 51 |
|
| 52 |
# Run the app
|
| 53 |
if __name__ == '__main__':
|
| 54 |
-
app.
|
| 55 |
|
| 56 |
|
| 57 |
|
|
|
|
| 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 |
+
# df['ZIP']=df['ZIP'].astype(str)
|
| 8 |
+
# df.to_parquet("C:/Users/aammann/OneDrive - Great American Insurance Group/Documents/cincinnati_traffic_crash_data__cpd.parquet")
|
| 9 |
+
df = pd.read_parquet('C:/Users/aammann/OneDrive - Great American Insurance Group/Documents/cincinnati_traffic_crash_data__cpd.parquet')
|
| 10 |
|
| 11 |
+
# Preprocess the data
|
| 12 |
+
df_grouped = df.groupby('SNA_NEIGHBORHOOD')['DATECRASHREPORTED'].count().reset_index()
|
| 13 |
+
df_grouped.columns = ['Neighborhood', 'Total Crashes']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Initialize the Dash app
|
| 16 |
app = Dash(__name__)
|
| 17 |
|
| 18 |
+
# Create a bar chart
|
| 19 |
+
fig = px.bar(df_grouped, x='Neighborhood', y='Total Crashes', title='Total Traffic Crashes by Neighborhood in Cincinnati')
|
| 20 |
+
|
| 21 |
# Define the layout of the app
|
| 22 |
+
app.layout = html.Div(children=[
|
| 23 |
+
html.H1(children='Cincinnati Traffic Crashes Dashboard'),
|
| 24 |
+
|
| 25 |
+
html.Div(children='''Dash: A web application framework for your data.'''),
|
| 26 |
+
|
| 27 |
dcc.Dropdown(
|
| 28 |
+
id='neighborhood-dropdown',
|
| 29 |
+
options=[{'label': n, 'value': n} for n in df_grouped['Neighborhood']],
|
| 30 |
+
value=df_grouped['Neighborhood'][0]
|
| 31 |
),
|
| 32 |
+
|
| 33 |
+
dcc.Graph(
|
| 34 |
+
id='example-graph',
|
| 35 |
+
figure=fig
|
| 36 |
+
)
|
| 37 |
])
|
| 38 |
|
| 39 |
+
# Callback to update graph based on dropdown selection
|
| 40 |
@app.callback(
|
| 41 |
+
Output('example-graph', 'figure'),
|
| 42 |
+
[Input('neighborhood-dropdown', 'value')]
|
| 43 |
)
|
| 44 |
+
def update_graph(selected_neighborhood):
|
| 45 |
+
filtered_df = df_grouped[df_grouped['Neighborhood'] == selected_neighborhood]
|
| 46 |
+
fig = px.bar(filtered_df, x='Neighborhood', y='Total Crashes', title=f'Total Traffic Crashes in {selected_neighborhood}')
|
| 47 |
return fig
|
| 48 |
|
| 49 |
# Run the app
|
| 50 |
if __name__ == '__main__':
|
| 51 |
+
app.run(debug=True, port=8051)
|
| 52 |
|
| 53 |
|
| 54 |
|