传递到 ID 为 "table" 的 DataTable 的无效参数数据

Invalid argument data passed into DataTable with ID "table"

我的仪表板上有我的表 1,所以我想在下面创建一个按钮来调出表 2。但是,我的回调中似乎有一些我无法弄清楚的错误。想知道错误是否恰好在 pie = ctx.triggered[0]['prop_id'].split('.')[0] 处。任何帮助将不胜感激! :)

这是我的代码

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import dash_table
from dash.dependencies import Input, Output
from engine import session
from model import datatable1,datatable2,datatable3
import plotly.express as px

table1 = pd.read_sql(session.query(datatable3).statement,session.bind)
table2 = pd.read_sql(session.query(datatable1.Name,datatable1.Number,datatable2.Address).join(datatable2).statement,session.bind)

app = dash.Dash(name, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True)

app.layout = html.Div([
html.Div(children=[html.Div([
                html.H1("Heading", style={'text-align': 'center', 'fontSize': 30}),

                dash_table.DataTable(  
                    columns=[{"name": i, "id": i,
                              'deletable': False,  
                              'renamable': True  
                              } for i in table1.columns],
                    data=table1.to_dict('records'), 
                    page_current=0,
                    page_size=20,
                    page_action='native',
                    sort_action='native',
                    column_selectable="single",
                    sort_mode='multi',
                    style_table={'overflowX': '',
                                 'maxHeight': '1000px'},
                    style_header={'backgroundColor': 'rgb(119, 184, 199)'},
                    style_cell={'backgroundColor': 'rgb(207,227,232)',
                                'color': 'black',
                                'whiteSpace': 'normal',
                                'height': 'auto',
                                'textAlign': 'left',
                                },
                    export_format='xlsx', 
                    export_headers='display',
                    merge_duplicate_headers=True,
                    filter_action='native',  
                    editable=True,  
                    row_deletable=True,  
                    sort_by=[]),

                dbc.Row(
                    [
                        dbc.Col(children=[html.Div([(html.Button('Press',id='test1button')),
                        dash_table.DataTable(id='table')])])])
           ])])
  ])

@app.callback(Output('table', 'data'),
        Input('test1button', 'n_clicks'))

def clicked_output(test1button):
    ctx = dash.callback_context

    if (test1button == None):
        return ''
    else:
        pie = ctx.triggered[0]['prop_id'].split('.')[0]
        if pie == 'test1button':
            return [
                dash_table.DataTable(
                    columns=[{"name": i, "id": i,
                              'deletable': False,  
                              'renamable': True 
                              } for i in table2.columns],
                    data=table2.to_dict('records')
                )
            ]

if name == "main":
    app.run_server(debug=True)

我的错误信息是

Invalid argument data passed into DataTable with ID "table". Expected an array. Was supplied type string. Value provided: ""

我不确定你想用上下文做什么,但你可以通过回调更简单地达到预期的结果:

    from dash.exceptions import PreventUpdate

    @app.callback(
        [Output("table", "data"),
         Output('table', 'columns')],
        Input('test1button', 'n_clicks')
    )
    def update_table(nclicks):
        """Retrieves data from the database

        Parameters
        ----------
        nclicks : int | None
            The number of times the button was pressed.
            Used to prevent initial update with empty state.

        Returns
        ------
        """
        if nclicks in [0, None]:
            raise PreventUpdate
        else:
            [data, columns] = get_data_and_columns()
            return [data, columns]

错误是由于默认情况下激活回调并且第一个 if 语句评估为 True。因此,函数 returns 是一个空字符串,因此会出现错误消息。您可以通过提高 PreventUpdate 来防止错误。