Py.Cafe

marie-anne/

dash-ibcs-experiment-2

AC, delta PY and PY%

DocsPricing
  • assets/
  • app.py
  • requirements.txt
  • ym_revenue_ai.csv
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 20 15:00:14 2025

@author: win11
"""

import dash as dash
from dash import dcc, html, Input, Output, callback
#import plotly.express as px
import plotly.graph_objects as go
#from data.dataprep import data_prep,create_ym_revenue,data_prep_ai_data
import pandas as pd
import numpy as np
import dash_bootstrap_components as dbc
#import plotly.io as pio


dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"

#df_money_order, df_money_orderdetail = data_prep()
#df_revenue_mom =  pd.read_csv('orderdetails_money.csv')
#df_revenue_mom = data_prep_ai_data().reset_index()
df_revenue_mom =  pd.read_csv('ym_revenue_ai.csv', index_col=False)
df_revenue_mom['Last_dayofmonth_Date'] = df_revenue_mom['Last_dayofmonth_Date'].apply(pd.to_datetime, format='%Y-%m-%d')



def create_bar_ac_py(df_revenue_mom):
    
     
    
    colors = {'AC': 'rgba(64,64,64,1)','PY':'rgba(166,166,166,1)' }
    # Define a small shift using Timedelta
    #shift = pd.Timedelta(days=3)  # Adjust as needed
    
    max_y = df_revenue_mom['revenue'].max()
    current_year = df_revenue_mom['Last_dayofmonth_Date'].dt.year.max()
    src_background = f"assets/{current_year}_figPY.svg"

    
    
    figPY = go.Figure()
    
    
    
    # First bar trace (shift left)
    figPY.add_trace(go.Bar(
               x=df_revenue_mom['Last_dayofmonth_Date'],
               y=df_revenue_mom['revenue_py'], 
               #width=0.5,
               marker_color=colors.get('PY'),
               name='PY'
    ))
    
    #background needs to be transparent
    figPY.update_layout({
        'plot_bgcolor': 'rgba(0, 0, 0, 0)',
        'paper_bgcolor': 'rgba(0, 0, 0, 0)',
    })
    
    
    
    # Change the bar mode
    figPY.update_layout(bargap=0.2)
    figPY.update_layout(yaxis_title=None)
    figPY.update_yaxes(showticklabels=False)
    figPY.update_layout(xaxis_title=None)
    figPY.update_xaxes(showticklabels=False)
    figPY.update_layout(yaxis_range=[0,max_y])
    figPY.update_layout(
        margin=dict(l=0, r=0, t=0, b=0),
    )
    
    #solution stuck kaleido 2 on win11 : https://github.com/plotly/Kaleido/issues/126
    #writing an image dynamically triggers the callback function for a second time
    #pio.write_image(figPY, "assets/figPY.svg") 
    
    figAC = go.Figure()
    
    # Second bar trace (shift right)
    figAC.add_trace(go.Bar(
               x=df_revenue_mom['Last_dayofmonth_Date'], 
               y=df_revenue_mom['revenue'],  
               #width=0.5,
               marker_color=colors.get('AC'),
               name='AC',
               text=df_revenue_mom['revenue'],
               texttemplate = "%{y:,.3s}"
    ))
    
    
    
    
    # Change the bar mode
    #figAC.update_layout(bargap=0.2)
    
    #background needs to be transparent fo a see through to PY
    figAC.update_layout({
         'plot_bgcolor': 'rgba(0, 0, 0, 0)',
         'paper_bgcolor': 'rgba(0, 0, 0, 0)',
     })
    
    figAC.update_xaxes(showgrid=False, zeroline=False)
    figAC.update_yaxes(showgrid=False, zeroline=False)
    
    # Add images
    figAC.add_layout_image(
            dict(
                source=src_background,
                xref="paper", yref="paper",
                #x=1, y=1,
                x=-0.01, y=0, 
                sizex=1,
                sizey=1,
                xanchor="left",
                yanchor="bottom",
                opacity=1,
                layer="below",
                sizing="stretch")
    )
    
    figAC.update_layout(yaxis_title=None)
    figAC.update_yaxes(showticklabels=False)
    figAC.update_xaxes(
        dtick="M1",
        tickformat="%b")
        #ticklabelmode="period")
        
    figAC.update_layout(
        margin=dict(l=0, r=0, t=0, b=0),
        autosize=False,
        width=800,
        height=300,
        hovermode=False
    )
    figAC.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False)
    


    return dcc.Graph(figure = figAC)


def create_delta_py(df_revenue_mom):
    
    colors = {'AC': 'rgba(64,64,64,1)','PY':'rgba(166,166,166,1)', 'red': 'rgba(255,0,0,1)', 'green':'rgba(0,142,150,1)' }
    
    
    figPY = go.Figure()
    
    
    
    # First bar trace (shift left)
    figPY.add_trace(go.Bar(
               x=df_revenue_mom['Last_dayofmonth_Date'],
               y=df_revenue_mom['delta_py'], 
               #width=0.5,
               #marker_color=colors.get('PY'),
               marker_color = df_revenue_mom['delta_py'].apply(lambda x: colors.get('red') if x < 0 else colors.get('green')) ,
               name='PY',
               text=df_revenue_mom['delta_py'],
               texttemplate = df_revenue_mom['delta_py'].apply(lambda x: "%{y:,.3s}" if x < 0 else "+%{y:,.3s}") 
    ))
    
    #background needs to be transparent
    figPY.update_layout({
        'plot_bgcolor': 'rgba(0, 0, 0, 0)',
        'paper_bgcolor': 'rgba(0, 0, 0, 0)',
    })
    
    
    
    # Change the bar mode
    figPY.update_layout(bargap=0.2)
    #figPY.update_layout(yaxis_title="<span style='rotate: 90deg;'>&#x394;PY</span>")
    figPY.update_layout(yaxis_title=None)
    figPY.update_yaxes(showticklabels=False)
    figPY.update_layout(xaxis_title=None)
    figPY.update_xaxes(showticklabels=False)

    figPY.update_layout(
        margin=dict(l=0, r=0, t=0, b=0),
        autosize=False,
        width=800,
        height=350,
        hovermode=False
    )
    
    figPY.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False)
    
    
    return dcc.Graph(figure = figPY)




def create_delta_py_perc(df_revenue_mom):
    
    colors = {'AC': 'rgba(64,64,64,1)','PY':'rgba(166,166,166,1)', 'red': 'rgba(255,0,0,1)', 'green':'rgba(0,142,150,1)' }
    
    
    figPY = go.Figure()
    
    
    
        
    #adding y=0
    #opacity + line_color make the lines appear nice on the white template as they are meant to be
    figPY.add_hline(y=0, line_dash="solid", line_width=2, opacity=1, line_color="Black")
    
    for x_val, y_val in zip(df_revenue_mom['Last_dayofmonth_Date'], df_revenue_mom['delta_perc_py']):
        figPY.add_shape(
            type="line",
            x0=x_val, x1=x_val,
            y0=0, y1=y_val,  # From y=0 to marker y-value
            line=dict(color=colors["red"] if y_val < 0 else colors["green"],  # Red for negative, green for positive
                      
                      
                      width=2)  # Adjust color and width as needed
    )
    
    
    
    
    
    
    figPY.add_trace(go.Scatter(
               x=df_revenue_mom['Last_dayofmonth_Date'],
               y=df_revenue_mom['delta_perc_py'],               
               marker = dict(color=colors['AC'],size=12),
               mode="markers + text",
               name="Percentage change with same month last year",
               text=df_revenue_mom['delta_py'],
               texttemplate = df_revenue_mom['delta_perc_py'].apply(lambda x: "%{y:.1%}" if x < 0 else "+%{y:.1%}") ,
               textposition= df_revenue_mom['delta_perc_py'].apply(lambda x: "bottom center" if x < 0 else "top center") 
 
    ))
    
    
    
    
    #background needs to be transparent
    figPY.update_layout({
        'plot_bgcolor': 'rgba(0, 0, 0, 0)',
        'paper_bgcolor': 'rgba(0, 0, 0, 0)',
    })
    
    
    
    # Change the bar mode
    figPY.update_layout(bargap=0.2)
    #figPY.update_layout(yaxis_title="<span style='rotate: 90deg;'>&#x394;PY%</span>" )
    figPY.update_layout(yaxis_title=None )
    figPY.update_yaxes(showticklabels=False)
    figPY.update_layout(xaxis_title=None)
    figPY.update_xaxes(showticklabels=False)

    figPY.update_layout(
        margin=dict(l=0, r=0, t=0, b=0),
        autosize=False,
        width=800,
        height=300,
        hovermode=False
    )
    
    figPY.update_traces(textfont_size=12, textfont_style='italic')


    
    return dcc.Graph(figure = figPY)





app = dash.Dash(__name__, external_stylesheets=[dbc.themes.SANDSTONE,dbc_css]) 

app.layout = dbc.Container([ 
    
                dbc.Row([
                    dbc.Col([
                        
                        html.P('Imaginary Traders'),
                        html.P('Revenue by year in K$'),
                        html.P( id='title'),
                        
                        
                        
                        ], className='header col-md-4'),
                    dbc.Col([
                        dbc.Select(
                            id="select_year",
                            options=[
                                {"label": "2024", "value": 2024},
                                {"label": "2023", "value": 2023}
                                ], value = 2024
                            )
                        
                        ], className='col-md-2'),
                    dbc.Col([ html.H2('IBCS Experiment no.2', style={'textAlign':'right'})])
                
                
                
                ], className='col-md-12'),
    
               dbc.Row([
                   dbc.Col([

                       html.Div([
                           html.Div("ΔPY%", className='title-border'),
                           html.Div(id='revenue_percentage')                           
                           ], className='minigrid'),
                       html.Div([
                           html.Div("ΔPY ", className='title-border'),
                           html.Div(id='revenue_money')
                           
                           
                           ], className='minigrid'),
                       html.Div([
                           html.Div([html.P('AC  ', style={'fontWeight':'bold'}),
                                     html.P('PY   ')], className='title-border'),
                           html.Div(id='total_revenue')
                           
                           
                           ], className='minigrid'),
                       

                       ]),


                   ]),
               dbc.Row([
                       dbc.Col(html.P('dash(2.18.2), dash-bootstrap-components(1.7.1), dash-bootstrap-templates 2.1.0', style={'fontSize':'9px','marginTop':'2rem'})
                                 ),
                        ]),
               
               ],style={'marginTop': '3rem', 'marginBottom': '2rem'},  fluid=False)

                            

@app.callback( Output('revenue_percentage', 'children'),
               Output('revenue_money', 'children'),
               Output('total_revenue', 'children'),
               Output('title', 'children'),
               Input(component_id='select_year', component_property='value')
              )


def update_all(value):
    


    #the incoming dataframe has all total, delta pm and delta pm% for each month,
    #filter it on the selected value (=year-month), drop=True to remove the extra
    #created useless index column
    
    if not value:
        data = df_revenue_mom[df_revenue_mom['Last_dayofmonth_Date'].dt.year == 2024]
    else:
        data = df_revenue_mom[df_revenue_mom['Last_dayofmonth_Date'].dt.year == int(value)]
     
    title = f"{value}, AC and PY by month"    
 
     
     #update all visuals with the updated data
    return  create_delta_py_perc(data),\
              create_delta_py(data),\
              create_bar_ac_py(data),\
                  title



app.run_server(debug=True)