Py.Cafe

XLlobet/

figure-friday-raleigh-permits-insights

Raleigh NC - Building Permits Insights

DocsPricing
  • figures/
  • 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from    dash                        import Dash, Input, Output, dcc, html
import  dash_bootstrap_components   as dbc
import  dash_customizable_app_style as dcas
import  plotly.express              as px
import  pandas                      as pd
from    datetime                    import datetime
from    figures                     import plots

df:     pd.DataFrame    = pd.read_csv("https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/main/2025/week-25/Building_Permits_Issued_Past_180_Days.csv")
df = df.dropna(subset=['fee'])

dates:          list            = [''.join(list(date)[:10]) for date in df['CreationDate']]
months:         list            = [datetime.strptime(date, "%Y/%m/%d").strftime("%B") for date in dates]
days_of_week:   list            = [datetime.strptime(date, "%Y/%m/%d").strftime("%A") for date in dates]

df['Month']                     = months
df['Week Day']                  = days_of_week

orders:         dict            = {'Week Day':  ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                                   "Month":     ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"],
                                   }


# Permits df
rows       = []

for value in orders['Month']:
    current_df          = df[df['Month'] == value]
    residential_df      = current_df[current_df['permitclassmapped'] == 'Residential']
    nonresidential_df   = current_df[current_df['permitclassmapped'] == 'Non-Residential']
    inside_city_df      = current_df[current_df['jurisdiction_inout_ral'] == 'Inside City Limits']
    outside_city_df     = current_df[current_df['jurisdiction_inout_ral'] == 'Outside City Limits']
    new_df              = current_df[current_df['workclassmapped'] == 'New']
    existing_df         = current_df[current_df['workclassmapped'] == 'Existing']

    rows.append({'Month':           value, 
                 'Permits Amount':  len(current_df), 
                 'Residential':     len(residential_df),
                 'Non-Residential': len(nonresidential_df),
                 'Inside City':     len(inside_city_df),
                 'Outside City':    len(outside_city_df),
                 'New':             len(new_df),
                 'Existing':        len(existing_df),
                 })

permits_df:     pd.DataFrame    = pd.DataFrame(rows)

cat_dict:       dict            = {'estprojectcost':    'Project Cost', 
                                   'fee':               'Fee', 
                                   'permitclassmapped': 'Permit Class',
                                   'Week Day':          'Week Day'}

cool_colors = [
    "#003f5c",  # deep navy blue
    "#006d5b",  # deep forest green
    "#5e548e",  # dark muted purple
    "#0077b6",  # vivid ocean blue
    "#00a86b",  # emerald green
    "#9d4edd",  # bright violet

    "#66cdaa",  # medium aquamarine
    "#b185db",  # pastel purple
    "#014f86",  # rich ocean blue
    "#5cb85c",  # leafy green
    "#a078b0",  # muted lilac
    "#90e0ef",  # icy blue
    "#8fd694",  # light moss green
]

grid                            = plots.create_grid(df)

app = Dash ("Figure Friday 2025 week 25", external_stylesheets=[dbc.themes.BOOTSTRAP])

# App layout
app.layout = html.Div(
    id       = "main_container",
    style    = {"minHeight": "100vh"},
    children = [
        dcas.customize_app_selectors(),
        html.H1("Raleigh NC - Building Permits Issued Past 180 Days", className="p-4 mb-0"),
        grid,
        html.Div(
            id          = "main_div",
            className   = "row m-0 p-0 pt-4 text-center",
            children    = [

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Residential Permits",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(f"{len(df[df['permitclassmapped'] == 'Residential'])}",
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Non-Residential Permits",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(f"{len(df[df['permitclassmapped'] == 'Non-Residential'])}",
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Status - Complete",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(f"{len(df[df['statuscurrent'] == 'Complete'])}",
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Status - Issued",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(f"{len(df[df['statuscurrent'] == 'Issued'])}",
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Average Cost",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(round(df['estprojectcost'].mean(), 1),
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "col-2 m-0 p-0 text-center",
                    children    = [
                        html.P("Average Fee",
                               className="m-0 p-0 fw-bold fst-italic fs-5",
                               style= {'color': '#4cb5c6'}),

                        html.P(round(df['fee'].mean(), 1),
                               className="m-0 p-0 fw-bold fs-1 fst-italic",
                               style= {'color': ' 	#b0e6d5'}),
                    ]),

                html.Div(
                    className   = "row m-0 p-0 text-center",
                    children    = [
                        
                        dcc.Dropdown(
                            className = "ms-4 me-4 mt-4 text-dark",
                            id = 'variable',
                            options = ['estprojectcost', 'fee'],
                            value = 'estprojectcost',
                        ),

                        html.Div(
                            className= "col-12 m-0 p-0",
                            children = [
                                dcc.Loading(
                                    children = [
                                        dcc.Store(id={"type": "figure-store", "index": "map_fig"}),
                                        dcc.Graph(
                                            id = {"type": "graph", "index": "map_fig"},
                                )], type='default'),
                            ]
                        ),

                        html.Div(
                            className= "col-12 m-0 p-0",
                            children = [
                                dcc.Loading(
                                    children = [
                                        dcc.Store(id={"type": "figure-store", "index": "summary"}),
                                        dcc.Graph(
                                            id = {"type": "graph", "index": "summary"},
                                            figure= px.histogram(permits_df, 
                                                                 x='Month', 
                                                                 y=['Permits Amount', 'Residential', 'Non-Residential', 'Inside City', 'Outside City', 'New', 'Existing'],
                                                                 barmode='group'),
                                )], type='default'),
                            ]
                        ),

                        

                        dcc.Dropdown(
                            className = "ms-4 me-4 mt-4 text-dark",
                            id = 'color',
                            options = ['Week Day', 'permitclassmapped'],
                            value = 'Week Day',
                        ),

                        html.Div(
                            className= "col-6 m-0 p-0",
                            children = [
                                dcc.Loading(
                                    children = [
                                        dcc.Store(id={"type": "figure-store", "index": "bar_week"}),
                                        dcc.Graph(
                                            id = {"type": "graph", "index": "bar_week"},
                                        )], type='default'),
                            ]
                        ),
                        
                        html.Div(
                            className= "col-6 m-0 p-0",
                            children = [
                                dcc.Loading(
                                    children = [
                                        dcc.Store(id={"type": "figure-store", "index": "bar_month"}),
                                        dcc.Graph(
                                            id = {"type": "graph", "index": "bar_month"},
                                        )], type='default'),
                            ]
                        ),

                    ])
                    
            ]),
    ])

@app.callback(
    Output({"type": "figure-store", "index": "summary"}, 'data'),
    Input('variable', 'value')
)
def update_figure(variable):
        
    fig     = px.bar( permits_df, 
                            x='Month', 
                            y=['Permits Amount', 'Residential', 'Non-Residential', 'Inside City', 'Outside City', 'New', 'Existing'],
                            barmode='group')
    
    return fig.to_dict()

@app.callback(
    Output({"type": "figure-store", "index": "map_fig"}, 'data'),
    Input('variable', 'value')
)
def update_figure(variable):
        
    fig_map         = px.scatter_map(df, 
                                     lat="latitude_perm", 
                                     lon="longitude_perm", 
                                     size=variable, 
                                     color=variable,
                                     map_style='carto-darkmatter',
                                     zoom=9.5, 
                                     height=550, 
                                     color_continuous_scale=px.colors.sequential.Plasma_r).update_layout(coloraxis_colorbar=dict(title=dict(text=f'{cat_dict[variable]}')), font=dict(size=10))
    
    return fig_map.to_dict()


@app.callback(
    Output({"type": "figure-store", "index": "bar_week"}, 'data'),
    Output({"type": "figure-store", "index": "bar_month"}, 'data'),
    Input('variable', 'value'),
    Input('color', 'value')
)
def update_bars(variable, color):

    if color == 'Week Day':

        color_seq = None

    else:

        color_seq = [cool_colors[0], cool_colors[12]]
    
    figure_day      = px.histogram( df, 
                                    x='Week Day',
                                    y=variable,
                                    title=f'{cat_dict[variable]} per {cat_dict[color]}',
                                    template='plotly_white',
                                    color=color,
                                    category_orders=orders,
                                    color_discrete_sequence=color_seq).update_layout(yaxis_title=f'{cat_dict[variable]}', font=dict(size=10))
    figure_month    = px.histogram( df, 
                                    x='Month',
                                    y=variable,
                                    title=f'{cat_dict[variable]} per Month and {cat_dict[color]}',
                                    template='plotly_white',
                                    color=color,
                                    category_orders=orders,
                                    color_discrete_sequence=color_seq).update_layout(yaxis_title=f'{cat_dict[variable]}', font=dict(size=10))

    return figure_day.to_dict(), figure_month.to_dict()


if __name__ == "__main__":
    app.run(debug=True)