【发布时间】:2019-04-24 22:23:42
【问题描述】:
我正在尝试使用 Inputs 在 Web 上创建破折号表。但是问题是数据是从回调和先验的数据库中创建的, 除非使用回调函数创建熊猫数据框,否则我不知道列的名称。 我已经检查过我得到了正确的数据。但是无法显示它。我使用了多个输出选项(使用 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'}, ......
不确定我该如何实现。任何帮助将不胜感激。
【问题讨论】:
-
同样的问题已经被问过here。
-
谢谢。但仍然没有得到任何合适的答案。希望让代码正常工作。
标签: python pandas plotly-dash