✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Update DataTable, store information in the Database and reload layout with new data

Hey guys,
I’m currently building my first dash app which also features user managment.
Now I also want to integrate the possibility to amend user information which is shown in a datatable on the admin page. With the following code I already managed to update the database as soon as the table is altered, however, once i leave the admin page and go back to it the table doesn’t fetch the updated database data but instead the old data before the change. Is there a way to get the layout to reload the data from the database once it’s been updated?

def UpdateAdminRights(app):
    @app.callback(
        Output('users', 'data'),
        [Input('users', 'data_timestamp')],
        [State('users', 'data')])
    def display_output(timestamp, df):
        old = show_users() #connects to the DB and fetches information
        changed_user = [change for user, change in zip(old,df) if user != change]
        if changed_user:
            username = changed_user[0]['username']
            email = changed_user[0]['email']
            admin = 1 if changed_user[0]['admin'] == "True" else 0
            update_user(username, email, admin) #connects to the DB and updates information
        return df

It’s triggered as soon as this table is altered:

            dbc.Container([
                html.H3('View Users'),
                html.Hr(),
                dbc.Row([
                    dbc.Col([
                        dt.DataTable(
                            id='users',
                            columns = [{'name' : 'ID', 'id' : 'id'},
                                        {'name' : 'Username', 'id' : 'username'},
                                        {'name' : 'Email', 'id' : 'email'},
                                        {'name' : 'Admin', 'id' : 'admin'}],
                            data=show_users(),
                            editable = True,
                        ),
                    ], md=12, className = 'twelve columns'),
                ]),
            ], className='pretty_container')