Change Dash Table Values and Columns based on dropdown values

All,

I have a dashtable that I am able to publish the server. However, I would to make its values and columns dynamic based on a dropdown callback I have.

import os
import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

df_daily= pd.read_excel(data, sheetname = ‘Daily’)
df_monthly= pd.read_excel(data, sheetname = ‘Monthly’)
df_qrt= pd.read_excel(data, sheetname = ‘Quarterly’)

app.layout = html.Div([
dcc.Dropdown(
id=‘LOB-dropdown’,
options=[{‘label’: k, ‘value’: k} for k in all_options.keys()],
value=‘Treasury Services’
),

html.Hr(),

dcc.Dropdown(id='time-period-dropdown'),


html.Hr(),

dt.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df_daily.columns],
data=df_daily.to_dict('records'),

)])

So this works, I can see the table. What I would like to do is tie the results to the below dropdowns:

@app.callback(
dash.dependencies.Output(‘time-period-dropdown’, ‘options’),
[dash.dependencies.Input(‘LOB-dropdown’, ‘value’)])
def set_time_period_options(selected_LOB):
return [{‘label’: i, ‘value’: i} for i in all_options[selected_LOB]]

@app.callback(
dash.dependencies.Output(‘time-period-dropdown’, ‘value’),
[dash.dependencies.Input(‘time-period-dropdown’, ‘options’)])
def set_time_period_value(available_options):
return available_options[0][‘value’]

Which I attempt by doing this:
@app.callback(
dash.dependencies.Output(“table”, “data”),
[dash.dependencies.Input(‘time-period-dropdown’,‘value’)])
def update_table(comments):
if comments == ‘TS Daily’ :
return dt.DataTable(
id=‘table’,
columns=[{“name”: i, “id”: i} for i in df_daily.columns],
data=df_daily.to_dict(‘records’))
elif comments == ‘TS Monthly’ :
return dt.DataTable(
id=‘table’,
columns=[{“name”: i, “id”: i} for i in df_monthly.columns],
data=df_monthly.to_dict(‘records’))

When it publishes and I make the second dropdown = TS Monthly - I get the below error:

The callback for property data
of component table returned a value
which is not JSON serializable.

                In general, Dash properties can only be
                dash components, strings, dictionaries, numbers, None,
                or lists of those.

What am I doing wrong?

Thank you

First of all for nicer formatting add triple ` (```) before and after your code :wink:

Your return value is a table, and not the data for a table, right?
So you are trying to return a table as data to a table.
I’m not 100% sure what the format needs to be, but I think you either need to supply a dictionary to a data table, or a data table to a div, but cannot supply a data table to a data table.