gpt4 book ai didi

python - 尝试使用多个输出选项创建表时出现 Dash 错误

转载 作者:行者123 更新时间:2023-12-01 07:58:16 29 4
gpt4 key购买 nike

我正在尝试使用输入在网络上创建破折号表。然而问题是数据是根据回调和先验从数据库创建的,我不知道列的名称,除非使用回调函数创建 pandas 数据框。我已经检查过我得到的数据是否正确。然而无法显示它。我使用了多个输出选项(使用 Dash 0.41)

我的代码如下所示:(我没有提供在回调someFunc中生成pandas数据帧的函数的详细信息,因为这对于 Dash 代码故障排除的目的来说并不重要。

 import dash_table as dt

def someFunc(ID, pattern_desc, file_path):

## do something
return df # pandas dataframe
#
 external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

server = app.server

app = dash.Dash(__name__)

app.config.suppress_callback_exceptions = True
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True


app.layout = html.Div(
children = [
html.Div(
id = 'title',
children = appTitle,
className = 'titleDiv'
),
html.Div(
children = [
html.Div(
children = "Enter ID:",
className = 'textDiv'
),
dcc.Input(
id = 'ID',
type = 'text',
value = 'ABCER1',
size = 8),

html.Div(
children = "Enter Test Pattern",
className = 'textDiv'
),
dcc.Input(
id = 'pattern_desc',
type = 'text',
value = 'Sample',
size = 20),

html.Div(
children = "Enter File OutPut Path:",
className = 'textDiv'
),
dcc.Input(
id = 'file_path',
type = 'text',
value = '',
size = 30),

html.Button(
id = 'submit',
n_clicks = 0,
children = 'Search'
)
]
),

html.Div(
id = 'tableDiv',
children = dash_table.DataTable(
id = 'table',
style_table={'overflowX': 'scroll'},
style_as_list_view=True,
style_header={'backgroundColor': 'white','fontWeight':
'bold'},
),
className = 'tableDiv'
)
]
)

# callback to update the table
@app.callback([Output('table', 'data'),Output('table', 'columns')]
[Input('submit', 'n_clicks')],
[State('ID', 'value'), State('pattern_desc', 'value'),
State('file_path', 'value')])
def update_table(n_clicks, ID, pattern_desc, file_path):

df = someFunc(ID, pattern_desc, file_path)
mycolumns = [{'name': i, 'id': i} for i in df.columns]
return html.Div([
dt.DataTable(
id='table',
columns=mycolumns,
data=df.to_dict("rows")
)
])

因此,在本例中,接受 3 个输入参数的函数 someFunc 返回一个 pandas 数据框,该数据框可以根据输入具有不同的列。因此应用程序布局应该显示这些列由回调函数的输出根据输入动态给出。我应该让网页填充表格和列,但却出现错误。当我运行这个时,我将通过函数生成的数据获取到文件中,但破折号无法在网页上生成表格。我收到以下错误:

dash.exceptions.InvalidCallbackReturnValue:回调..table.data...table.columns..是多输出。预期输出类型为列表或元组,但得到 Div([DataTable(columns=[{'name': 'pattern_desc', 'id': 'pattern_desc'}, ......

不确定如何才能实现这一目标。任何帮助将不胜感激。

最佳答案

错误消息相当简单。装饰器

`@app.callback([Output('table', 'data'),
Output('table', 'columns')], ...)`

指定 2 个输出,因此函数应返回元组 (data, columns) 而不是 html.Div(...) 值。

关于python - 尝试使用多个输出选项创建表时出现 Dash 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839364/

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