I am trying to do something so simple that I guess I am way off track:
To use a dropdown menu callback to select between two (or more) DataTables populated from entirely separate dataframes/sources.
Perhaps incorrectly, I initially generate each DataTable separately outside of the layout. This has worked for me when selecting between different figures (from different datasets) for the same Div.
I then select the relevant datatable basis the dropdown selection and the simple function: def update_data_table(value)
Note, I wondered if I was duplicating the DataTable operation by first using it outside the layout as dashtable.DataTable; and then using ddk.DataTable inside the design kit wrapper.
However, I return the same “Duplicate Callback error” if I replace ddk.DataTable with:
html.Div(id = ‘return_datatable’)
The errors returned are as follows. one for each part of the same DataTable output:
Duplicate callback outputs
In the callback for output(s):
datatable_1.data
datatable_1.columns
Output 1 (datatable_1.columns) is already in use.
Any given output can only have one callback that sets it.
To resolve this situation, try combining these into
one callback function, distinguishing the trigger
by using dash.callback_context
if necessary.
Duplicate callback outputs
In the callback for output(s):
datatable_1.data
datatable_1.columns
Output 0 (datatable_1.data) is already in use.
Any given output can only have one callback that sets it.
To resolve this situation, try combining these into
one callback function, distinguishing the trigger
by using dash.callback_context
if necessary.
Generalized code is as follows:
data_cols_1 = df_1.columns
d_columns_1 = [{‘name’: x, ‘id’: x} for x in data_cols_1]
d_table_1 = dash_table.DataTable(
# Set up the columns and data
columns=d_columns_1,
data=d_columns_1.to_dict(‘records’),
cell_selectable=False,
# Set up sort, filter and pagination
sort_action=‘native’,
filter_action=‘native’,
page_action=‘native’,
page_current=0,
page_size=12,
)
data_cols_2 = df_2.columns
d_columns_2 = [{‘name’: x, ‘id’: x} for x in data_cols_2]
d_table_2 = dash_table.DataTable(
# Set up the columns and data
columns=d_columns_2,
data=d_columns_2.to_dict(‘records’),
cell_selectable=False,
# Set up sort, filter and pagination
sort_action=‘native’,
filter_action=‘native’,
page_action=‘native’,
page_current=0,
page_size=12,
)
def serve_layout():
return [
ddk.Row(children=[
ddk.Card(children=[
ddk.CardHeader(children=[
dcc.Dropdown(
id=‘table-dropdown’,
options=[{‘label’: i, ‘value’: i}
for i in [‘Option_1’, ‘Option_2’]],
value=‘Option_1’
)
]),
ddk.DataTable(id=‘return_datatable’)
]),
]),
]
@app.callback([Output(‘return_datatable’, ‘data’), Output(‘return_datatable’, ‘columns’)],
[Input(‘table-dropdown’, ‘value’)])
def update_data_table(value):
if value == ‘Option_1’:
table = d_table_1
else:
table = d_table_2
return table