gpt4 book ai didi

python - 从带有 Plotly Dash for Python 的回调中将 Pandas DataFrame 作为 data_table 返回

转载 作者:行者123 更新时间:2023-12-04 11:38:06 37 4
gpt4 key购买 nike

我想读取一个 .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)

最佳答案

当您尝试注册回调时 Output组件作为 DataTableDataTable 的所有必需/强制属性组件应该在回调中更新并返回。在您的代码中,您只更新 DataTable.data而不是 DataTable.column ,一种简单的方法是返回整个 Datatable预填充了所有必需属性值的组件。

这是一个例子,

import dash_html_components as html
import dash_core_components as dcc
import dash
import dash_table
import pandas as pd
import dash_table_experiments as dt

app = dash.Dash(__name__)

#data to be loaded
data = [['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'),
columns=df.columns,
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=list(df.index), # all rows selected by default
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()

return [
dt.DataTable(
rows=df_chart.to_dict('rows'),
columns=df_chart.columns,
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=list(df_chart.index), # all rows selected by default
id='3'
)
]

if __name__ == '__main__':
app.run_server(debug=True)


看起来像 dash-table-experiments已弃用。

编辑 1:这是使用 dash_tables 实现的一种方法
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
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([
dt.DataTable(
id = 'dt1',
columns = [{"name": i, "id": i,} for i in (df.columns)],

),
html.Div([
html.Button(id='submit-button',
children='Submit'
)
]),

])

@app.callback(Output('dt1','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()
data_1 = df.to_dict('rows')
return data_1

if __name__ == '__main__':
application.run(debug=False, port=8080)

另一种方式:返回整个 DataTable
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
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([
html.Div(id="table1"),

html.Div([
html.Button(id='submit-button',
children='Submit'
)
]),

])

@app.callback(Output('table1','children'),
[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()
data = df.to_dict('rows')
columns = [{"name": i, "id": i,} for i in (df.columns)]
return dt.DataTable(data=data, columns=columns)


if __name__ == '__main__':
application.run(debug=False, port=8080)



我引用了这个例子:
https://github.com/plotly/dash-table/blob/master/tests/cypress/dash/v_copy_paste.py#L33

关于python - 从带有 Plotly Dash for Python 的回调中将 Pandas DataFrame 作为 data_table 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55269763/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com