Display different message based on the row data in datatable

Hi,

How to display different message based on the row data in the datatable?

df:

Customer Product Sale reference Payment Date
Customer 1 Desktop sale001 08/11/2022
Customer 2 Laptop sale002a 08/11/2022
Customer 3 Keyboard saleof003 11/11/2022
Customer 4 Mouse trn_004
Customer 5 Pendrive trn_005
Customer 6 Hard disk trn_for_006 01/12/2022
Customer 7 Mouse pad sale007 13/12/2022

expected output1:

Customer Customer 1
Product Desktop
Sale reference sale001
Payment Date 08/11/2022
Message:
Completed

expected output2:

Customer Customer 4
Product Mouse
Sale reference trn_004
Payment Date
Message:
Incompleted

code:

product_cat = list(df['Product'].unique()) 

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col(html.H1("Sale record")   
                ),
      
        ]),
    html.Br(),
    html.Br(),
    
    dbc.Row([
       dbc.Col([
           html.H3('Product')
           
           ]),
       
       dbc.Col([            
           dcc.Dropdown(id='product_dd', value= None, #df['year'].max(),
                          options = [{'label':x, 'value':x} 
                                    for x in product_cat],
                          
                          searchable = True, search_value='',
                          placeholder= 'Please select ...',
                          clearable= True
                          ),
           
           ]),    
       
       
       ]),
    html.Br(),
      
    dbc.Row([
        dbc.Col([
            html.P("Details:"),
            
                                    
            dash_table.DataTable(id='tabletrn', 
                                    columns=df.columns,
                                                                  
                                  ),
           
                ]),
            ]),   
    html.Br(),
          
    dbc.Row([
        dbc.Col([
            html.P('Message:'),
            html.Div(id='message'
                
                
                # dbc.Alert(id='message'
                #                # , is_open = True
                #                )
                     
                     
                     )
            
            ]),
        ]),

    
])




# #TABLE

@app.callback(
    Output('tabletrn', 'data'), 
    Input('product_dd', 'value'),

    )


def update_table(selection):
    if len (selection) == 0:
        return dash.no_update
    
    else:
     
        dff = df[(df['Product'] == selection)]
    
        
        info = dff.melt(id_vars = ['Reference_ID']
                  ,var_name = ['header']
                  ,value_name = 'info')
        
        
         labels = {'Customer': 1, 'Product': 2 , 'Sale reference': 3 , 'Payment Date': 4}
    

        info['labels']= info['header'].apply(lambda x: labels[x]) 

        info['ref_info'] = info['info'].fillna('No')       
                    
        
        columns = info[['header','info']]

        
        data= columns.to_dict('records')

       
        return data
        

   
@app.callback(    
    Output('message', 'children'), 
    Input('tabletrn', 'data')
    )



def label_race (row):             
    if ((row['labels'] in {4}) & (row['ref_info'] != 'No')) :
         return dbc.Alert([html.P('noop')])

    
    elif ((row['labels'] == 17) & (row['ref_info'] == 'No')):
         return dbc.Alert([html.P('yess')])
        

if __name__ == "__main__":
    app.run_server(debug=True)

i had tried but it will show an error message

Callback error updating message.children
TypeError: list indices must be integers or slices, not str

Hi, an educated guess: row or any other variable you might use is a list, so you can’t do something like this:

row['ref_info']

The error message is pretty clear. :upside_down_face:

Hi @AIMPED,

Do you have others idea that I can get the same result (display different message based on row datatable)?

I had tried to to a test which the result is in df basis, it can print the message, therefore I use the same way in datatable, but came out error message :sob:

Hi @beginof, what I would do in this case is adding a html.div(id=“dummy”) to the layout and write a callback returning a json.dumps(row) to the dummy so that I can see what’s the content of row.

Hi @AIMPED,

Do you have any sample or can you provide some sample code?