My post above shows you all the things you would need to add, just to confirm did you add all 3 parts? If you didn’t do all parts and only did the one you copy pasted it’s not going to work without the rest.
For the columns that’s my mistake you can delete the column line completely you do not need to define the column names when you’re returning the data as records. Alternatively you can manually define each column and it’s data type. Manually defining will give you more fine control of the data table but obviously is a bit more work.
It explains all of this in the Datatable documentations:
Here’s a sample app to return data from callback to dataframe. As noted there’s may changes you’ll need to make to work with your application layout like the button spinner and such. But this is the callback logic to pass data to the table. How I set it up will display the full DF in the datatable when no dropdown is selected, then if you pick one of the fruits in the dropdown the table filtered down to that fruit only.
from dash import Dash, dcc, html, dash_table, Input, Output, State
import pandas as pd
app = Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(['Apples','Oranges','Bananas'], id='input1'),
dash_table.DataTable(
id='table-name',
data=[],
)
])
@app.callback(
Output('table-name', 'data'),
Input('input1', 'value'),
)
def update_table(input1):
#Do the data processing/SQL pull etc below I will just use a sample df for the example:
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})
if input1:
df = df[df['Fruit']==input1]
return df.to_dict('records')
else:
return df.to_dict('records')
if __name__ == '__main__':
app.run_server(debug=True)
Thanks
Payton