Append Row to Datatable

I have specific data I’m trying to append to a datatable, and every cell in the row will be different. I’ve seen examples of how to append an empty row, but I am unsure of how to do this with real data.

Thank you!

Below is the code to append an empty row:

@app.callback(
    Output('table', 'data'),
    [Input('add-row-button', 'n_clicks')],
    [State('table', 'data'),
    State('table', 'columns')]
)
def add_row(n_clicks, rows, columns):
        rows.append({c['id']: '' for c in columns})
    return rows

@Chris7 can you share some of your data. If it’s confidential, can you create a similar sample data?

1 Like

just in case it’s helpful, here’s an example of adding new rows to a DataTable.

1 Like

Hi Adam! Thank you for the link; I will try out this implementation and see if it suits my needs better, but I was actually able to find another way to append a row to a datatable. Below is an example for anyone who may find it helpful.

# Layout
columns = [
     {'id': 'value1', 'name': 'Column 1'},
     {'id': 'value2', 'name': 'Column 2'},
     {'id': 'value3', 'name': 'Column 3'},
],
data=[]


#Callback
@app.callback(
    Output('table', 'data'),
    [Input('add-row-button', 'n_clicks')],
    [State('table', 'data'),
    State('table', 'columns')]
)
def add_row(n_clicks, rows, columns):
     data = {'value1':my_var1, 'value2':my_var2, 'value3':my_var3}
     rows.append({c['id'] = data[c['id']] for c in columns})

     # Note: You can also create a pandas dataframe from the datatable with this:
     df = pd.DataFrame(rows, columns=[c['id'] for c in columns])

     return rows

@adamschroeder It’s great to see you on the forum; your videos have been extremely helpful! I’ll definitely be reading your book soon too.

And I hate to bother you with this here, but just off the top of your head, would you happen to know of a way to download a Dash page as an html? No worries if not. I asked this question a little while ago but am still unsure.

Thank you again for all of your help with Plotly Dash!

1 Like

hi @Chris7
I’m glad you found an answer. Regarding your HTML question, there might be a hacky way of doing this, but exporting to standalone HTML is not yet supported.

1 Like