Obtaining information from datatable selected_rows

Hi team,

I’m trying to update 2 dropdowns using value from a datatable (single selection only), upon a row been selected. I’m running into error after selecting the row. I’m wondering if I am not using the “selected_rows” property right.

Many thank for any help.

@app.callback(
    [Output('dropdown1', 'value'),
     Output('dropdown2','value')
     ],
    [Input('datatable', 'data'),
     Input('datatable', 'selected_rows')
     ])

def update_dropdown(data,selected_rows):
    dropdown1=data[selected_rows]['column1']
    dropdown2=data[selected_rows]['column2']

    return dropdown1, dropdown2

Hi @Felton
What’s the error message that you get?

Is ‘columm1’ the exact name of the column?

Tip-
Right above the dropdown1=… code line, try print(selected_rows) and see what you get.

Hi Adam thanks for the reply.

The error msg is “TypeError: list indices must be integers or slices, not NoneType”. Which suggest the value of selected_rows is empty. My table looks like the below.

Column name is correct as when i substitute selected_rows with a number, eg 1, the code works.

ss

I’m not sure why that is happening. If you select a row, and you print(selected_rows), you get an empty list?

I solved it. Turned out the selected_row was returning a list where I thought it was an integer. So instead of
dropdown1=data[selected_rows][‘column1’]

I had to change it to
dropdown1=data[selected_rows[0]][‘column1’]

Thanks for your replies Adam.

It’s a good idea to always print(x) or print(type(x)) to see what the input looks like.
I’m glad you solved it.