Py.Cafe

jackparmer/

dash-fire-department-calls-analysis

Fire Department Calls Analysis

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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)