✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Save EDITED dash data table back to database

hi. I added an extra rows to the data-table and now I want to save back to the database and each time when I add the rows it should be saved in database.

How can I do this? Please suggest me.

import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_html_components as html
import pandas as pd

app = dash.Dash(name)

data = pd.DataFrame({
‘x’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’],
‘y’: [4, 3, 1, 2, 3, 6],
‘z’: [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
})

app.layout = html.Div([
html.Div([
html.Button(‘Save’, id=‘save-rows-button’, n_clicks=0)
], style={‘height’: 50}),

dash_table.DataTable(
    id='adding-rows-table',
    columns=[{
        'name': '{}'.format(i),
        'id': '{}'.format(i),
        'deletable': True,
        'editable_name': True,
         'resizable':True,
    }for i in data], 
    data = data.to_dict('records'),
    editable=True,
    row_deletable=True,
    sorting = True,
    ),

html.Button('Add Row', id='editing-rows-button', n_clicks=0),

])

@app.callback(
Output(‘adding-rows-table’, ‘data’),
[Input(‘editing-rows-button’, ‘n_clicks’)],
[State(‘adding-rows-table’, ‘data’),
State(‘adding-rows-table’, ‘columns’)])
def add_row(n_clicks, rows, columns):
if n_clicks > 0:
rows.append({c[‘id’]: ‘’ for c in columns})
return rows

@app.callback(
Output(‘adding-rows-table’, ‘columns’),
[Input(‘save-rows-button’, ‘n_clicks’)])
def update_columns(n_clicks, save_file):
if n_clicks > 0:

    'save edited dash table to database'
    
return save_file

if name == ‘main’:
app.run_server(debug = True)

Hey, did you every find a solution for this?

Thanks,

Hi kittu410,

How did you solve this?