Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Updating DataTable based on user selection

Hello, I currently have an empty DataTable that allows the user to input their own numbers for graphing. However, I would like to offer the user the option to select a sample dataset to be input into the DataTable automatically for them.

How can I accomplish this?

My current code looks like this:

dash_table.DataTable(
        id='prod_table',
        columns=(
            [{'id': p, 'name': p} for p in params]
        ),
        data=[
            dict(Model=i, **{param: 0 for param in params})
            for i in range(1, 10)
        ])

Hi,

Welcome to the community!

I believe you’ve already seen how to make editable datatables, as your code snippet looks very similar to the example there.

Now, if you want to replace the entire data to a pre-defined sample when the user interact with a component (say, a button), you can simply add a callback to return the data property of your table as the sampled data. Something like:

@app.callback(
    Output("prod_table", "data"),
    Input("button-id", "n_clicks"
)
def add_sample_data_to_table(n_clicks):
    if n_clicks is not None: # or prevent_initial_update=True in signature
        return sample_dataset

Just make sure that the sample dataset has the same column names as params.

Hope this helps!