Py.Cafe

Mike-Purtell/

dash-nyc-taxi-application-status

NYC Taxi Application Status

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
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
import polars as pl
import plotly.express as px
from dash import Dash, dcc, html, Input, Output
import dash_bootstrap_components as dbc

#----- GLOBAL DATA STRUCTURES --------------------------------------------------
eval_cols = [
    'DRUG_TEST','WAV_COURSE','DEFENSIVE_DRIVING', 'DRIVER_EXAM',
    'MEDICAL_CLEARANCE'
]

color_dict = {
    'COMPLETE' : 'mediumspringgreen',
    'INCOMPLETE' : 'lightcoral'
}

style_space = {
    'border': 'none',
    'height': '4px', 
    'background': 'linear-gradient(to right, #007bff, #ff7b00)', 
    'margin': '10px,',
    'fontsize': 32
}

source_data = (
    'https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/' +
    'main/2025/week-19/TLC_New_Driver_Application.csv'
)

#----- CALLBACK FUNCTION ---- --------------------------------------------------
def get_bar_chart(df, completed_val):
    data_len = dict_complete_len.get(completed_val)
    df_bar = (
        df
        .select(pl.col('REQUIREMENT',completed_val))
        .rename({completed_val:'COMPLETE'})
        .with_columns(
            INCOMPLETE = data_len - pl.col('COMPLETE')
        )
    )

    bar_chart = px.bar(
        df_bar, 
        y='REQUIREMENT', 
        x=['COMPLETE', 'INCOMPLETE'], 
        # height=600, width=900, 
        template='simple_white',
        title=(
            f'COMPLETED {completed_val} of {requirement_num} REQUIREMENTS<br>' +
            f'<sup>Status of {data_len} Applicants</sup>'
        ),
        color_discrete_map=color_dict,
    )
    bar_chart.update_layout(
        yaxis=dict(autorange="reversed"),
        font_size=20,
        legend_title_text='Status'
    )
    bar_chart.update_xaxes(title_text='APPLICANTS')
    bar_chart.update_yaxes(title_text='')
    return(bar_chart)

#----- READ & CLEAN DATASET ----------------------------------------------------
df = (
    pl
    .scan_csv(source_data)
    .select(
        APP_DATE = pl.col('App Date').str.to_date(format='%m/%d/%Y'),
        STATUS = pl.col('Status')
            .str.replace('Approved - License Issued', 'Approved')
            .str.replace('Pending Fitness Interview', 'Fitness_Interview'),
        DRUG_TEST = pl.when(pl.col('Drug Test') == 'Complete')
                    .then(pl.lit(1))
                    .otherwise(pl.lit(0)),
        WAV_COURSE = pl.when(pl.col('WAV Course') == 'Complete')
                .then(pl.lit(1))
                .otherwise(pl.lit(0)),
        DEFENSIVE_DRIVING = pl.when(pl.col('Defensive Driving') == 'Complete')
                .then(pl.lit(1))
                .otherwise(pl.lit(0)),
        DRIVER_EXAM = pl.when(pl.col('Driver Exam') == 'Complete')
                .then(pl.lit(1))
                .otherwise(pl.lit(0)),
        MEDICAL_CLEARANCE = pl.when(pl.col('Medical Clearance Form') == 'Complete')
                .then(pl.lit(1))
                .otherwise(pl.lit(0)),
    )
    .filter(pl.col('APP_DATE') >= pl.datetime(2024,1,1)) # exclude pre-2024
    # count occurances of 'Complete in 5 listed columns
    .with_columns(COMPLETES = pl.sum_horizontal(eval_cols))
    .select(
        DRUG_TEST=pl.col('DRUG_TEST')
            .sum()
            .over('COMPLETES'),
        WAV_COURSE=pl.col('WAV_COURSE')
            .sum()
            .over('COMPLETES'),
        DEFENSIVE_DRIVING=pl.col('DEFENSIVE_DRIVING')
            .sum()
            .over('COMPLETES'),
        DRIVER_EXAM=pl.col('DRIVER_EXAM')
            .sum()
            .over('COMPLETES'),
        MEDICAL_CLEARANCE=pl.col('MEDICAL_CLEARANCE')
            .sum()
            .over('COMPLETES'),
        COMPLETES = pl.sum_horizontal(eval_cols)     
    )
    .with_columns(
        LEN=pl.col('DRUG_TEST')
            .count()
            .over('COMPLETES'),         
    )
    .with_columns(pl.col('COMPLETES').cast(pl.String))
    .unique('COMPLETES')
    .sort('COMPLETES')
    .collect()
)

dict_complete_len = dict(zip(df['COMPLETES'], df['LEN']))
completes_list = list(dict_complete_len.keys())
df_transpose = (
    df
    .transpose(
        include_header=True, 
        column_names='COMPLETES',
        header_name='REQUIREMENT', 
    )
    .filter(pl.col('REQUIREMENT') != 'LEN')
    .sort('REQUIREMENT')
)
requirement_num = df_transpose.height

app = Dash(external_stylesheets=[dbc.themes.LUX])
app.layout =  dbc.Container([
    html.Hr(),
    html.H2(
        'NYC Taxi Application Status', 
        style={'text-align': 'center', 'font-size': '32px'}
    ),
    html.Hr(style=style_space),
    html.Div([
        html.P(
            'Radio buttons select # of completed requirements', 
            style={
                'text-align': 'center', 
                'margin-left': '80px', 
                'margin-right': '80px', 
                'font-size': '32px',
                'color': 'gray',
            }
        ),
        html.Hr(style=style_space)
    ]),
    dbc.Col(
        [
            dbc.RadioItems(
                id='radio_sel',
                options=completes_list,
                value = '4',
                inline=True,
                labelStyle={
                    'margin-right': '30px',  
                    'font-size': '20px',
                }
            )
        ]
    ),
    html.Div(id='dd-output-container'),
    dbc.Row([
        dbc.Col(dcc.Graph(id='bar_chart')),
    ]),
])
#----- DASH APPLICATION STRUCTURE-----------------------------------------------
@app.callback(
    Output('bar_chart', 'figure'),
    Input('radio_sel', 'value'),
)
def update_dashboard(completed_val):
    return (
        get_bar_chart(df_transpose, completed_val)
    )
if __name__ == '__main__':
    app.run_server(debug=True)