I have dataframe with few columns. I will select a column from a dropdown option, and return a dataframe, where the new dataframe will contain 2 variable from original dataframe and an additional calculated column. Then it will display as datatable.
The sample code as follows:
column_options = [dict(label=x, value=x) for x in df.columns]
#This function gets the parameter of a column , create a range based on values and assign it to a new column, and return a dataframe
def get_df(param):
dr=df[[‘column1’,param]]
cr=dr.columns[1]+’_range’
dr[cr]=pandas.cut(x=dr[dr.columns[1]], bins=range(0,100+25,25), right=False)
rt=dr[[dr[2],dr.columns[1],dr.columns[0]]]
return rt
The generated dataframe looks like as follows:
app.layout = html.Div(
html.H3("Category"),
html.Div(
[
html.Div('Select Variable'),
html.Div(dcc.Dropdown(id='my_dropdown',
options=column_options
, value='column1'))
],
style={"width": "25%", "float": "left"},
),
html.Div(
dash_table.DataTable(
id='datatab'),
style={'height': 750, 'overflowY': 'scroll'},
className='six columns'
),
)
#It selects a column, passes the column
@app.callback(Output(‘datatab’, ‘rows’), [Input(‘my_dropdown’, ‘value’)])
def make_datatab(value):
r=get_df(value)
return r.to_dict(‘records’)
When I run it, it shows error:
dash.exceptions.InvalidCallbackReturnValue:
The callback for property `rows`
of component `drugcat` 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.
I have tried all possible ways.
What is the problem here? How can I resolve it?