here is the code that currently I use to display the table without the function:
customer = list(df['Customer'].unique())
app.layout = dbc.Container([
dbc.Row([
dbc.Col(html.H1("2nd page",
className = 'text-center text-primary, mb-4 '
,style={"textDecoration":"underline",'font-weight': 'bold'}),
width=12
),
]),
html.Br(),
html.Br(),
dbc.Row([
dbc.Col([
html.H3('Customer'
,style={'font-size': '25px'}
),
], width=3, md=4),
dbc.Col([
dcc.Dropdown(id='customer_dd', value= '',
options = [{'label':x, 'value':x}
for x in customer],
searchable = True, search_value='',
placeholder= 'Please select ...',
clearable= True
),
html.Br(),
], width=3, md=4),
]),
html.Br(),
dbc.Row([
dbc.Col([
html.H3('Sale Date from'
,style={'font-size': '25px'}
),
], width=3, md=4),
dbc.Col([
dcc.Dropdown(id='date_from', value= '',
searchable = True, search_value='',
placeholder= 'Please select ...',
clearable=True
),
html.Br(),
], width=3, md=4),
],),
html.Br(),
dbc.Row([
dbc.Col([
html.H3('Sale Date to'
,style={'font-size': '25px'}
),
], width=3, md=4),
dbc.Col([
dcc.Dropdown(id='date_to', value= '',
searchable = True, search_value='',
placeholder= 'Please select ...',
clearable=True
),
html.Br(),
], width=3, md=4),
], ),
html.Br(),
dbc.Row([
dbc.Col([
html.P("Table:",
style={"textDecoration":"underline"}),
dash_table.DataTable(id='tabledetail',
columns=[
{'name': 'Customer', "id": 'Customer'},
{'name': 'Product', "id": 'Product'},
{'name': 'Sale Reference', "id": 'Sale Reference'},
{'name': 'Status', "id": 'header'},
{'name': 'Date', "id": 'info'},
{'name': 'Action', "id": 'action',},
# {'name': 'Action', "id": 'action','type':'text', 'presentation':'markdown'},
{'name': 'Reference', "id": 'reference'},
],
editable=False,
style_cell={'textAlign': 'left'},
active_cell = {"row": 0, "column": 0, "column_id": "Merchant ID", "row_id": 0}
),
]),
]),
])
@app.callback(
Output('date_from','options'),
Input('customer_dd', 'value')
)
def update_dd (merchant_dd):
date = df.drop_duplicates(['Sale Date'], inplace= False)
relevant_date = date[ df['Customer'] == merchant_dd]['Sale Date'].values.tolist()
date_option= [dict(label=x,value=x)for x in relevant_date]
return date_option
@app.callback(
Output('date_to','options'),
Input('customer_dd', 'value')
)
def update_dd (merchant_dd):
date = df.drop_duplicates(['Sale Date'], inplace= False)
relevant_date = date[ df['Customer'] == merchant_dd]['Sale Date'].values.tolist()
date_option= [dict(label=x,value=x)for x in relevant_date]
return date_option
@app.callback(
Output('tabledetail', 'data'),
Input('customer', 'value'),
Input('date_from', 'value'),
Input('date_to', 'value')
)
def update_table(selection, datefr, dateto):
if len (selection) == 0:
return dash.no_update
else:
dff = df[df['Customer'] == selection]
daterange = dff[(dff['Sale Date']>=datefr)
& (dff['Sale Date']<=dateto)
]
transpose = (daterange.set_index(['Customer', 'Product', 'Sale Reference']).rename_axis(columns='header')
.stack(dropna=False).reset_index(name='info')
)
info = transpose
info['ref_info'] = info['info'].fillna('No')
info = info[info['ref_info'] !='No' ]
#######
# today = datetime.now()
# if today.hour > 15:
# today = today + timedelta(days=1)
# today = today.date()
#######
tr1 = info[info['header'].str.contains(pat = 'Sale Date', regex = True)]
tr1['action'] = 'Make Payment'
tr2 = info[info['header'].str.contains(pat = 'Payment Date', regex = True)]
tr2['reference'] = 'user id & time'
final_df1 = pd.merge(left = info ,right = tr1, how="left", left_on=[
'Customer', 'Product', 'Sale Reference', 'header', 'info','ref_info'], right_on=[
'Customer', 'Product', 'Sale Reference','header', 'info','ref_info']).fillna('')
final_df2 = pd.merge(left = final_df1 ,right = tr2, how="left", left_on=[
'Customer', 'Product', 'Sale Reference', 'header', 'info','ref_info'], right_on=[
'Customer', 'Product', 'Sale Reference','header', 'info','ref_info']).fillna('')
columns = final_df2[['Customer', 'Product', 'Sale Reference', 'header','info', 'action','reference' ]]
data= columns.to_dict('records')
return data