Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Updating Part of Rows of Same Dash Datatable

Hi All,

I am trying to update part of rows of the same dash datatable. The example in the documentation shows how this can be done for columns (https://dash.plot.ly/datatable/editable), and I have figured out how this can be implemented for rows:

params_years = [1,2,3,4,5,6,7]
id_PL = ['Test']
params_PL = [1,2,3,4,5,6,7,8,9,10]

dash_table.DataTable(
    id='adding-rows-table',
    columns=(
            [{'id': 'Test', 'name': 'Test', 'editable': False}] +
            [{'id': p, 'name': p} for p in params_years]
    ),
    data=[
        dict({param: params_PL[i] for param in id_PL}) for i in range(0, len(params_PL))

    ],
    editable=True,
    row_deletable=True,
),

@app.callback(
Output('adding-rows-table', 'data'),
[Input('adding-rows-table', 'data_timestamp')],
[State('adding-rows-table', 'data')])
def update_rows(timestamp, rows):
sum_test = {k: int(rows[0].get(k, 0)) -
                            int(rows[1].get(k, 0)) +
                            int(rows[2].get(k, 0)) +
                            int(rows[3].get(k, 0)) +
                            int(rows[4].get(k, 0)) +
                            int(rows[5].get(k, 0))
                         for k in set(rows[0])}

rows[6] = sum_test
return rows

This works, but I don’t want params_PL to be integers. If I change params_PL to Strings, I get an error as Dash tries to add Strings together (which obviously fails). Does anyone know how I can make this work, while still using Strings in the left column, instead of Integers?

I think Dash rows are seen as dictionaries, but I just couldn’t get it to work. Many thanks for your help!

After taking another look at it, I found the solution. For anyone who encounters the same issue, I did the following:

Added a copy of rows[0] as you don’t want the for-loop to go through the whole set(rows[0]). Then remove the first element of the dictionary as that one contains the String

rows_copy = copy.deepcopy(rows[0])
rows_copy.pop('Test')

Set the for-statement to loop through rows_copy instead of rows[0]:

sum_test = {k: int(rows[0].get(k, 0)) -
                            int(rows[1].get(k, 0)) +
                            int(rows[2].get(k, 0)) +
                            int(rows[3].get(k, 0)) +
                            int(rows[4].get(k, 0)) +
                            int(rows[5].get(k, 0))
                         for k in set(rows_copy)}

Add the first element back again

sum_test['Test'] = 'Stringvalue'
rows[6] = sum_test

This change worked for me and I can now add parts of rows in the same Dash Datatable. Awesome Datatable btw!