-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.py
171 lines (146 loc) · 4.83 KB
/
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
# LL PRO BONUS: R SHINY APPLICATION ----
# BUSINESS SCIENCE LEARNING LABS ----
# LAB 59: CUSTOMER LIFETIME VALUE | PYTHON DASH ----
# ----
# LIBRARIES
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
import numpy as np
import pathlib
# APP SETUP
external_stylesheets = [dbc.themes.CYBORG]
app = dash.Dash(
__name__,
external_stylesheets=external_stylesheets
)
PLOT_BACKGROUND = 'rgba(0,0,0,0)'
PLOT_FONT_COLOR = 'white'
LOGO = "https://www.business-science.io/img/business-science-logo.png"
# PATHS
BASE_PATH = pathlib.Path(__file__).parent.resolve()
ART_PATH = BASE_PATH.joinpath("artifacts").resolve()
# DATA
predictions_df = pd.read_pickle(ART_PATH.joinpath("predictions_df.pkl"))
df = predictions_df \
.assign(
spend_actual_vs_pred = lambda x: x['spend_90_total'] - x['pred_spend']
)
# LAYOUT
# Slider Marks
x = np.linspace(df['spend_actual_vs_pred'].min(), df['spend_actual_vs_pred'].max(), 10, dtype=int)
x = x.round(0)
navbar = dbc.Navbar(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(html.Img(src=LOGO, height="30px")),
dbc.Col(dbc.NavbarBrand("Customer Spend Prediction", className="ml-2")),
],
align="center",
no_gutters=True,
),
href="https://www.business-science.io/",
),
dbc.NavbarToggler(id="navbar-toggler", n_clicks=0),
dbc.Collapse(
id="navbar-collapse", navbar=True, is_open=False
),
],
color="dark",
dark=True,
)
app.layout = html.Div(
children = [
navbar,
dbc.Row(
[
dbc.Col(
[
html.H3("Welcome to the Customer Analytics Dashboard"),
html.Div(
id="intro",
children="Explore Customers by Predicted Spend versus Actual Spend during the 90-day evaluation period.",
),
html.Br(),
html.Hr(),
html.H5("Spend Actual vs Predicted"),
html.P("Segment Customers that were predicted to spend but didn't. Then target these customers with targeted emails."),
dcc.Slider(
id = 'spend-slider',
value = df['spend_actual_vs_pred'].max(),
max = df['spend_actual_vs_pred'].max(),
min = df['spend_actual_vs_pred'].min(),
marks = {i: '$'+str(i) for i in range(x[0],x[-1]) if i % 300 == 0}
),
html.Br(),
html.Button("Download Segmentation", id="btn"), dcc.Download(id="download")
],
width = 3,
style={'margin':'10px'}
),
dbc.Col(
dcc.Graph(id='graph-slider'),
width = 8
)
]
)
]
)
# CALLBACKS
@app.callback(
Output('graph-slider', 'figure'),
Input('spend-slider', 'value'))
def update_figure(spend_delta_max):
df_filtered = df[df['spend_actual_vs_pred'] <= spend_delta_max]
fig = px.scatter(
data_frame=df_filtered,
x = 'frequency',
y = 'pred_prob',
color = 'spend_actual_vs_pred',
color_continuous_midpoint=0,
opacity=0.5,
color_continuous_scale='IceFire',
hover_name='customer_id',
hover_data=['spend_90_total', 'pred_spend'],
) \
.update_layout(
{
'plot_bgcolor': PLOT_BACKGROUND,
'paper_bgcolor':PLOT_BACKGROUND,
'font_color': PLOT_FONT_COLOR,
'height':700
}
) \
.update_traces(
marker = dict(size = 12)
)
return fig
# Download Button
@app.callback(
Output("download", "data"),
Input("btn", "n_clicks"),
State('spend-slider', 'value'),
prevent_initial_call=True,
)
def func(n_clicks, spend_delta_max):
df_filtered = df[df['spend_actual_vs_pred'] <= spend_delta_max]
return dcc.send_data_frame(df_filtered.to_csv, "customer_segmentation.csv")
# Navbar
@app.callback(
Output("navbar-collapse", "is_open"),
[Input("navbar-toggler", "n_clicks")],
[State("navbar-collapse", "is_open")],
)
def toggle_navbar_collapse(n, is_open):
if n:
return not is_open
return is_open
if __name__ == '__main__':
app.run_server(debug=True)