I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table. Is this possible with Dash? 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
app = dash.Dash()
application = app.server
app.layout = html.Div([
dash_table.DataTable(
id = 'datatable',
),
html.Div([
html.H3('Give path and name of .csv file:',style=dict(paddingRight='30px')),
dcc.Input(
id='csv-files-loc',
style={'fontSize':12}
),
html.Button(id='submit-button',
children='Submit'
)
]),
])
@app.callback(Output('datatable','data'),
[Input('submit-button','n_clicks')],
[State('csv-files-loc','value')])
def update_datatable(n_clicks,csv_file):
if n_clicks:
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
'c78bf172206ce24f77d6363a2d754b59/raw/'
'c353e8ef842413cae56ae3920b8fd78468aa4cb2/'
'usa-agricultural-exports-2011.csv')
dfgb = df.groupby(['state']).sum()
return dfgb.to_dict('rows')
if __name__ == '__main__':
application.run(debug=False, port=8080)