我想读取一个.csv文件,并返回一个groupby函数作为回调,以使用“ dash_table”库显示为简单数据表。 @Lawliet的有用答案显示了如何使用“ dash_table_experiments”库执行此操作。这是我被困住的地方:
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)
答案 0 :(得分:4)
当您尝试将回调 Output
组件注册为 DataTable
时, DataTable
组件的所有必需/必需属性都应为在回调中更新并返回。在您的代码中,您只更新了 DataTable.data
,而不是 DataTable.column
,一种简单的方法是返回整个 Datatable
组件,即预填充所有必需的属性值。
这里是一个例子,
将dash_html_components导入为html
导入dash_core_components作为dcc
导入破折号
导入dash_table
将熊猫作为pd导入
导入dash_table_experiments作为dt
app =破折号(__name__)
#要加载的数据
数据= [['Alex',10],['Bob',12],['Clarke',13],['Alex',100]]
df = pd.DataFrame(data,columns = ['Name','Mark'])
app.layout = html.Div([
dt.DataTable(
rows = df.to_dict('records'),
column = df.columns,
row_selectable =真实,
filterable =真,
sortable =真,
selected_row_indices = list(df.index),#默认情况下选中的所有行
id ='2'
),
html.Button('Submit',id ='button'),
html.Div(id =“ div-1”),
])
@ app.callback(
dash.dependencies.Output('div-1','children'),
[dash.dependencies.Input('button','n_clicks')])
def update_output(n_clicks):
df_chart = df.groupby('Name')。sum()
返回[
dt.DataTable(
rows = df_chart.to_dict('rows'),
column = df_chart.columns,
row_selectable =真实,
filterable =真,
sortable =真,
selected_row_indices = list(df_chart.index),#默认情况下选中的所有行
id ='3'
)
]
如果__name__ =='__main__':
app.run_server(debug = True)
不推荐使用 dash-table-experiments
。
编辑1:这是使用 dash_tables
将pandas导入为pd
导入破折号
导入dash_core_components作为dcc
将dash_html_components导入为html
将dash_table导入为dt
从dash.dependencies导入输入,输出,状态
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
'c78bf172206ce24f77d6363a2d754b59 / raw /'
'c353e8ef842413cae56ae3920b8fd78468aa4cb2 /'
'USA-agricultural-exports-2011.csv')
应用= dash.Dash()
应用程序= app.server
app.layout = html.Div([
dt.DataTable(
id ='dt1',
列= [{(“名称”:i,“ id”:i,}代表(df.columns)中的i],
),
html.Div([
html.Button(id ='submit-button',
children ='提交'
)
]),
])
@ app.callback(Output('dt1','data'),
[Input('submit-button','n_clicks')],
[状态('提交按钮','n_clicks')])
def update_datatable(n_clicks,csv_file):
如果n_clicks:
dfgb = df.groupby(['state'])。sum()
data_1 = df.to_dict('rows')
返回data_1
如果__name__ =='__main__':
application.run(调试= False,端口= 8080)
另一种方式:返回整个 DataTable
将pandas导入为pd
导入破折号
导入dash_core_components作为dcc
将dash_html_components导入为html
将dash_table导入为dt
从dash.dependencies导入输入,输出,状态
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
'c78bf172206ce24f77d6363a2d754b59 / raw /'
'c353e8ef842413cae56ae3920b8fd78468aa4cb2 /'
'USA-agricultural-exports-2011.csv')
应用= dash.Dash()
应用程序= app.server
app.layout = html.Div([
html.Div(id =“ table1”),
html.Div([
html.Button(id ='submit-button',
children ='提交'
)
]),
])
@ app.callback(Output('table1','children'),
[Input('submit-button','n_clicks')],
[状态('提交按钮','n_clicks')])
def update_datatable(n_clicks,csv_file):
如果n_clicks:
dfgb = df.groupby(['state'])。sum()
数据= df.to_dict('行')
column = [{“ name”:i,“ id”:i,}(在(df.columns)中为i)
返回dt.DataTable(data = data,column = columns)
如果__name__ =='__main__':
application.run(调试= False,端口= 8080)
答案 1 :(得分:2)
您只需在update_datatable
中进行少量修改就可以完成它,它应该可以正常工作(未经测试):
def update_datatable(n_clicks,csv_file):
if n_clicks:
dfgb = df.groupby(['state']).sum()
return html.Div([dash_table.DataTable(
data=dfgb.to_dict('rows'),
columns=[{'name': i, 'id': i} for i in dfgb.columns],
style_header={'backgroundColor': "#FFD700",
'fontWeight': 'bold',
'textAlign': 'center',},
style_table={'overflowX': 'scroll'},
style_cell={'minWidth': '180px', 'width': '180px',
'maxWidth': '180px','whiteSpace': 'normal'},
filtering=True,
row_selectable="multi",
n_fixed_rows=1),
html.Hr()
])