Link/button in dash_table.DataTable

Hi,

Is it possible to create a own link or button to attach in the dash_table.DataTable?
The link or button has able to get the date (as per the below function) and the date received should able to update back to existing database column.

workflow that needed:
link/button in table > user click it > get date > date received will update back to database > table show

function that work to get the date:

today = datetime.now()

if today.hour > 15:
    today = today + timedelta(days=1)

today = today.date()


(original) df:

Customer Product Sale reference Sale Date Payment Date
Customer 123 Clothes sale001 08/11/2022
Customer ABC Laptop sale002 08/11/2022

(original) dash_table.DataTable:




Action: user will click the ‘Make payment’ >





(expected output) df:

Customer Product Sale reference Sale Date Payment Date
Customer 123 Clothes sale001 08/11/2022 09/12/2022
Customer ABC Laptop sale002 08/11/2022 09/12/2022

(expected output) dash_table.DataTable:

1 Like

Yes, this is possible. Albeit with some custom JavaScript.

To work, we need to have a hidden button in the layout that will act as the trigger to update the info in the background.

Then add an event listener to the specific cell that you want to make the payment on.

Maybe interesting in this context:

Hi @jinnyzor,

Can you share some guide how to do this?

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

HI @AIMPED,

Can the active cell only for specified data in the specified column and row?

HI @beginof , I am sorry, I don’t understand your question.

Hi @AIMPED,

for example, only the ‘make payment’ in the action is the active cell or able to click in the table.

Customer Product Sale reference Status Date Action
Customer 123 Clothes sale001 Sale date 08/11/2022 Make payment
Customer 123 Clothes sale no 123 Payment date 09/11/2022 Paid

Taking @adamschroeder’s example:

You can restrict the action of “active_cell” to whatever columns/cells you want. If you wanted to restrict the graph showing up to Angola only, you could do something like that:

    if country == 'Angola':
        fig = px.line(
            df[df["country"] == country], x="year", y=y, title=" ".join([country, y])
        )
        fig.update_layout(title={"font_size": 20},  title_x=0.5, margin=dict(t=190, r=15, l=5, b=5))
        fig.update_traces(line=dict(color=color[y]))

        return dcc.Graph(figure=fig)
    return []

Basically wrapping lines 59-65 of the original code in an if- statement.

1 Like