import dash
import pandas as pd
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
# Initialize the Dash app
app = dash.Dash(__name__)
data = pd.read_csv('https://gist.githubusercontent.com/jackparmer/e5549cac08ea7e27e18861a2aa8bad1d/raw/8b72e51c16f2fa114b8c44fd77f6c1f852e964c7/gistfile1.txt')
# Initialize the Dash app with Bootstrap CSS
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# Layout of the Dash app
app.layout = dbc.Container(
[
dbc.Row(
dbc.Col(html.H2("Fire Department Calls for Service"), width={'size': 6, 'offset': 3}),
className="my-4"
),
dbc.Row(
[
dbc.Col(
[
dbc.Row(
[
dbc.Label("Date Range", html_for="date-picker-range"),
dcc.DatePickerRange(
id='date-picker-range',
start_date=data['Call Date'].min(),
end_date=data['Call Date'].max(),
display_format='YYYY-MM-DD',
style={'width': '100%'}
),
],
className="mb-3",
),
dbc.Row(
[
dbc.Label("Call Type", html_for="call-type-dropdown"),
dcc.Dropdown(
id='call-type-dropdown',
options=[{'label': call_type, 'value': call_type} for call_type in data['Call Type'].unique()],
multi=True,
placeholder="Select Call Type",
style={'width': '100%'}
),
],
className="mb-3",
),
dbc.Row(
[
dbc.Label("Fire Prevention District", html_for="district-dropdown"),
dcc.Dropdown(
id='district-dropdown',
options=[{'label': district, 'value': district} for district in data['Fire Prevention Districts'].unique()],
multi=True,
placeholder="Select Fire Prevention District",
style={'width': '100%'}
),
],
className="mb-3",
),
],
width=3,
),
dbc.Col(
[
dcc.Graph(id='bar-chart', style={'height': '45vh'}),
dcc.Graph(id='line-chart', style={'height': '45vh'})
],
width=9,
),
]
)
],
fluid=True,
)
# Callback to update the visualizations based on filters
@app.callback(
[Output('bar-chart', 'figure'),
Output('line-chart', 'figure')],
[Input('date-picker-range', 'start_date'),
Input('date-picker-range', 'end_date'),
Input('call-type-dropdown', 'value'),
Input('district-dropdown', 'value')]
)
def update_visualizations(start_date, end_date, call_types, districts):
# Filter the data based on selections
filtered_df = data.copy()
if start_date and end_date:
filtered_df = filtered_df[(filtered_df['Call Date'] >= start_date) & (filtered_df['Call Date'] <= end_date)]
if call_types:
filtered_df = filtered_df[filtered_df['Call Type'].isin(call_types)]
if districts:
filtered_df = filtered_df[filtered_df['Fire Prevention Districts'].isin(districts)]
# Create the bar chart for call types
bar_fig = px.bar(filtered_df['Call Type'].value_counts().reset_index(), x='index', y='Call Type',
labels={'index': 'Call Type', 'Call Type': 'Count'},
title="Distribution of Call Types")
# Create the line chart for temporal trends
filtered_df['Call Date'] = pd.to_datetime(filtered_df['Call Date'])
line_fig = px.line(filtered_df.resample('M', on='Call Date').size().reset_index(name='Count'),
x='Call Date', y='Count',
title="Monthly Trend of Calls")
return bar_fig, line_fig
# Run the Dash app
if __name__ == '__main__':
app.run_server(debug=True)