I looked at this answer: DatePickerRange to Update Data Table
But I think there is an error in it as there is no df.to_dict(‘rows’). It probably should be 'records'
to update data
in a DataTable
But I get the point that data and columns need to update separately. So I reorganized my layout and callbacks and i’m still failing silently.
Part of my layout:
DataTable(
id="conversion-table",
data=[],
style_cell={'textAlign': 'left'},
style_data_conditional=[{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(248,248,248)'
}],
style_header={
'backgroundColor': 'rgb(230,230,230)',
'fontWeight': 'bold'
},
),
html.Div(
html.H5("Nutrients")
),
html.Br(),
DataTable(
id="nutrients-table",
data=[],
style_cell_conditional=[{
'if': {'column_id': c},
'textAlign': 'left'
} for c in ['Name']
],
style_data_conditional=[{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(248,248,248)'
}],
style_header={
'backgroundColor': 'rgb(230,230,230)',
'fontWeight': 'bold'
},
),
and my callbacks, not even to update but just to show the tables as they start off blank with data=[]
me thinks.
@app.callback(
Output('table-foodgroup-source', 'children'),
Output('conversion-table', 'data'),
Output('conversions-table', 'columns'),
Output('nutrients-table', 'data'),
Output('nutrients-table', 'columns'),
Input('search-ingredient', 'value')
)
def show_tables(ingredient):
'''
return top table, conversions table, nutrients table, yield table,
refuse table
'''
food_id = food_to_id_dict[ingredient]
food = CNFFoodName.objects.get(id=str(food_id))
food_grp = food.food_group.name
food_src = food.food_source.description
food_sci_name = "n/a"
if food.scientific_name:
food_sci_name = food.scientific_name
# table for food group, source, scientific name
food_group_table = html.Table([
html.Thead([
html.Tr([html.Th("Group"), html.Th("Source"), html.Th("Scientific Name")])
]),
html.Tbody([
html.Tr([
html.Td(food_grp),html.Td(food_src),html.Td(food_sci_name)
])
])
])
conversions_df = make_conversions_df(food_id)
#conversions_cols = [{"id": i, "name": conversions_df.columns[i]} for i in range(len(conversions_df.columns))]
conversions_cols = [{"name": i, "id": i} for i in conversions_df.columns]
conversions_data = conversions_df.to_dict('records')
nutrients_df = make_nutrients_df(food_id)
#nutrients_cols = [{"id": i, "name": nutrients_df.columns[i]} for i in range(len(nutrients_df.columns))]
nutrients_cols = [{"name": i, "id": i} for i in nutrients_df.columns]
nutrients_data = nutrients_df.to_dict('records')
#nutrients_table = make_nutrients_table(nutrients_df)
return food_group_table, conversions_data, conversions_cols,\
nutrients_data, nutrients_cols
Not even that first food-group table that is a plaint html.Table
is outputting. My other callback that depends on the same input is working. This is sad.