我们如何使用提供数据框的函数使用回调在Dash Table中动态创建数据列

Sta*_*tan 9 html css python pandas plotly-dash

我正在尝试使用Inputs在Web上创建破折号表。但是问题在于,数据是通过回调和先验从数据库创建的,除非使用回调函数创建了pandas dataframe,否则我不知道列的名称。我已经检查过我获得了正确的数据。但是无法显示它。我使用了多个输出选项(使用Dash 0.41)

我的代码如下所示:(我未提供在回调someFunc中生成熊猫数据帧的函数的详细信息,因为对于Dash代码TroubleShooting而言,这并不重要。

 import dash_table as dt

 def someFunc(ID, pattern_desc, file_path):

       ## do something 
      return df # pandas dataframe
Run Code Online (Sandbox Code Playgroud)
 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")
         )
        ])
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,带有 3个输入参数的函数someFunc返回一个pandas数据,该数据可以基于输入具有不同的列。因此,应用布局应根据输入动态显示回调函数的输出所给定的那些列。我应该用表格和列填充网页,但是却出现错误。运行此命令时,我将通过函数生成的数据获取到文件中,但是破折号无法生成网页上的表。我收到以下错误:

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

不确定如何实现。任何帮助将不胜感激。

cor*_*nda 0

如果我理解正确,那么您可以简单地创建另一个回调来输出道具的更新值columns。您还可以使用多输出回调同时更新两者。

@app.callback(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):
    mydata = someFunc(ID, pattern_desc, file_path)
    # here you would use the dataframe columns to create the new column values
    return new_column_values
Run Code Online (Sandbox Code Playgroud)