I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table with "dash_table" library. @Lawliet's helpful answer shows how to do that with "dash_table_experiments" library. Here is where I’m stuck:
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
'c78bf172206ce24f77d6363a2d754b59/raw/'
'c353e8ef842413cae56ae3920b8fd78468aa4cb2/'
'usa-agricultural-exports-2011.csv')
app = dash.Dash()
application = app.server
app.layout = html.Div([
dash_table.DataTable(
id = 'datatable',
),
html.Div([
html.Button(id='submit-button',
children='Submit'
)
]),
])
@app.callback(Output('datatable','data'),
[Input('submit-button','n_clicks')],
[State('submit-button','n_clicks')])
def update_datatable(n_clicks,csv_file):
if n_clicks:
dfgb = df.groupby(['state']).sum()
return dfgb.to_dict('rows')
if __name__ == '__main__':
application.run(debug=False, port=8080)
You almost got it done just with minor modification in
update_datatable
it should work fine (not tested):When you are trying to register the callback
Output
component as aDataTable
, all the required / mandatory attributes for theDataTable
component should be updated in the callback and returned. In your code, you are updating justDataTable.data
and notDataTable.column
, one easy way is to return the wholeDatatable
component which is prepopulated with all the required attribute values.Here is an example,
Looks like
dash-table-experiments
is deprecated.Edit 1: Here is one way of how it can be achieved using
dash_tables
Another way: return the whole
DataTable
I referred to this example: https://github.com/plotly/dash-table/blob/master/tests/cypress/dash/v_copy_paste.py#L33