Edited DataTable write to SQL on button click

Hello everyone, I’m trying to pass an edited dataframe between callbacks but am not really sure how to do so. I need to get df from find() to updated_output so I can do some further processing. I’m not sure if I should be using dcc.Store?


def serve_layout():
    testdict, partlist = testq(linertestq, csubdict)
    df2= pd.DataFrame(partlist.items(), columns =['material', 'csub'])
    df2['id'] = df2['material']
    df2.set_index('id', inplace=True, drop=False)
    return html.Div([
        html.H1(children="", style={'textAlign':'center', 'font-family': 'Comic Sans MS'}),
        html.H1(children="Enter Quantity", style={'textAlign':'center', 'font-family': 'Comic Sans MS'}),
        dcc.Input(id="inputpn", autoComplete='off', type="text", placeholder="Part Number"),
        dcc.Input(id="deactid", autoComplete='off', type="number", debounce=True, placeholder="Deactivation Lot Number",),
        dcc.Input(id="tech", autoComplete='off', type="text", debounce=True, placeholder="Technician",),
        html.Hr(),
        html.Div(id="packbreakdown"),
        html.Div(
            dcc.ConfirmDialogProvider(
            children=html.Button('Submit',),
            id='packsubmit',
            message='Values have been updated?'),
            style={'margin-left':'25%', 'textAlign':'center', 'width':'50%'}),

        html.H1(children="", style={'textAlign':'center', 'font-family': 'Comic Sans MS'}),
    ])

layout = serve_layout()

@app.callback(
    Output('packbreakdown', "children"),
    Input('inputpn', "value"),
    Input('deactid', "value"))
def find(pn,deactid):
    testdict, partlist = testq(linertestq, csubdict)
    tempdict = {}
    packsize = []
    if pn == None:
        return
    print (testdict[pn])
    tempdict[pn] = testdict[pn][str(deactid)]
    total, redlist = linerplan.packanalyze(packdict, packplan, tempdict)
    for item in total:
        packsize.append(packdict[item])
    df = pd.DataFrame(total.items(), columns =['material', 'count'])
    df['id'] = df['material']
    print (packsize)
    df['pack size'] = packsize
    # Create a pandas Series object with all the column values passed as a Python list
    s_row = pd.Series(['Scrap', 0, 0, 1], index=df.columns)
    df = df.append(s_row,ignore_index=True)
    df.set_index('id', inplace=True, drop=False)
    return html.Div(
            dt.DataTable(
            id='packbreakdown',
            columns=[
                {'name': i, 'id': i, 'deletable': True} for i in df.columns
                # omit the id column
                if i != 'id'
            ],
            data=df.to_dict('records'),
            editable=True,
            filter_action="native",
            sort_action="native",
            sort_mode='multi',
            row_deletable=True,
            selected_rows=[],
            page_action='native',
            page_current= 0,
            page_size= 10),
        style={'margin-left':'25%', 'textAlign':'center', 'width':'50%'})

@app.callback(
    Output('confirmsplash', 'children'),
    Input('tech', 'value'),
    Input('df', ;data))
  
def updated_output(check):
    PROCESS DATATABLE in here

Hi,

If the second callback needs df from the first callback as input, then you should refer to it as Input("packbreakdown", "data") in the second callback, not ("df", "data"). Note that the data won’t no longer be a pd.DataFrame, but a list of dictionaries.