How to put an object created inside the callback inside the app.layout?

I created some buttons inside a data table. Both the data table and buttons were created inside the callback. The buttons have ids, which I used as Inputs in other callbacks. The buttons and callbacks and everything work fine, but I keep receiving this error: A nonexistent object was used in an Input of a Dash callback. The id of this object is knw-open-4 and the property is n_clicks. I understand that this error stems from the fact that the buttons are not explicitly in the app.layout.

Is it possible to reference the buttons in the layout to avoid the error? If so, how? If not, what can I do?

Here is my code for the layout and buttons:

layout = html.Div(
      [     
            (...),
            html.Div(
                      "test table for data",
                      id="knowledge_test_table"
             )
      ]

@app.callback(
    [
        Output('knowledge_test_table', 'children')
    ],
    [
        Input('url', 'pathname')
    ]
)
def knowledge_datatable(pathname):
    if pathname == '/directory/knowledge':        
        sql = """ SELECT knw_id, knw_name, knw_delete_ind
        FROM knowledge knw
        WHERE knw_delete_ind = false
        """
        values = []
        cols = ['Knowledge ID','Knowledge Name', 'Delete Indicator']
        df = db.querydatafromdatabase(sql, values, cols)
        
        linkcolumn = {}
        for index, row in df.iterrows():
            if row['Delete Indicator'] == False:
                linkcolumn[index] = dbc.Button(
                    'Edit',
                    href='/directory/knowledge?mode=edit&knw_id='+str(row["Knowledge ID"]),
                    id=f"knw-open-{index}",                
                    )
                
                dictionarydata = {'Edit': linkcolumn}

                data_dict = df.to_dict()
                data_dict.update(dictionarydata)
                df = pd.DataFrame.from_dict(data_dict)

            df = df[['Knowledge Name', 'Edit']]
        
        table = dbc.Table.from_dataframe(df, striped=True, bordered=True, hover=True, size='sm', id="dashtable")
        return [table]
        
    else:
        raise PreventUpdate        

The buttons are linkcolumn[index]

(I’m not sure if it’s worth noting, but I have app.config.suppress_callback_exceptions = True). Hope someone can help, thanks!

Hi @kutsiinta,

Welcome to the community!

The best alternative to avoid this error is to define app.validation_layout as explained here. You can add all buttons and other components that will eventually be rendered in the app layout there, with no particular structure or order.

Hope that this helps!