【问题标题】:How to fix this Dash datatable callback error?如何修复此 Dash 数据表回调错误?
【发布时间】:2020-12-31 13:39:04
【问题描述】:

我有两个来自下拉菜单的回调,它们应该指向一个数据表。有一个由入口代码、汽车类型和年份组成的数据库。下拉菜单将允许您选择汽车,然后选择年份,生成的数据表应该是所有所选汽车和所有比所选年份更新的汽车的列表。

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

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

dataa = [[‘ABQ’,‘Toyota’,2015],[‘QRC’,‘Honda’,2016],[‘BFG’,‘Honda’,2017],[‘AAA’,‘Toyota’,2018],[‘KLX’,‘Toyota’,2020]]
dframe = pd.DataFrame(dataa,columns=[‘Entry’,‘Car’, ‘Year’])

app.layout = html.Div([

html.Label("Name:", style={'fontSize':70, 'textAlign':'center'}),

dcc.Dropdown(
    id='cars-dpdn',
    options=[{'label': s, 'value': s} for s in sorted(dframe.Car.unique())],

    clearable=False
),

html.Label("Year", style={'fontSize':30, 'textAlign':'center'}),
dcc.Dropdown(
    id='years-dpdn', 
    options=[],
),

html.Label("Results:", style={'fontSize':70, 'textAlign':'center'}),

html.Div(id="table1")
])

@app.callback(
Output(‘years-dpdn’, ‘options’),
Input(‘cars-dpdn’, ‘value’)
)
def years(chosen_car):
dff = dframe[dframe.Car==chosen_car]
return [{‘label’: c, ‘value’: c} for c in sorted(dff.Year.unique())]

@app.callback(
Output(‘years-dpdn’, ‘value’),
Input(‘years-dpdn’, ‘options’)
)
def years_value(available_options):
return [x[‘value’] for x in available_options]

@app.callback(
Output(‘table1’, ‘figure’),
Input(‘cars-dpdn’, ‘value’),
Input(‘years-dpdn’, ‘value’),
)

def update_table(car, year):

dfa = dframe[dframe['Car']==car]
dfb = dfa[dfa['Year']>year]

return html.Div([dash_table.DataTable(
    data=dfb.to_dict('rows'),
    columns=[{'name': i, 'id': i} for i in dfb.columns],
    ),
    html.Hr()
                ])
if name == ‘main’:
app.run_server(debug=False, port = 3060)

下拉菜单功能正常,允许我选择 Car 和 Year 值,但是没有创建表。它只是空白。错误信息很冗长,但这里是结尾:

ValueError: Lengths must match to compare

127.0.0.1 - - [30/Dec/2020 20:45:00] “POST /_dash-update-component HTTP/1.1” 500 - 
127.0.0.1 - - [30/Dec/2020 20:45:00] “POST /_dash-update-component HTTP/1.1” 200 - 
127.0.0.1 - - [30/Dec/2020 20:45:02] “POST /_dash-update-component HTTP/1.1” 200 -

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    如果您可以分享错误的完整回溯,这将有所帮助。您如何定义回调存在问题。 Inputs 都应该用方括号括起来。例如,您有:

    @app.callback(
        Output(‘table1’, ‘figure’),
        Input(‘cars-dpdn’, ‘value’),
        Input(‘years-dpdn’, ‘value’),
    )
    

    但应该是:

    @app.callback(
        Output(‘table1’, ‘figure’),
        [
          Input(‘cars-dpdn’, ‘value’),
          Input(‘years-dpdn’, ‘value’),
        ]
    )
    

    即使只有一个Input,也必须如此。否则 Dash 会抛出错误。

    这个回调看起来也很可疑:

    @app.callback(
        Output(‘years-dpdn’, ‘value’),
        Input(‘years-dpdn’, ‘options’)
    )
    def years_value(available_options):
        return [x[‘value’] for x in available_options]
    

    您正在输出到下拉列表的 value 道具,但该道具不会接受列表,因此您应该只从您创建的列表中给它一个项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2020-02-22
      • 2014-03-01
      相关资源
      最近更新 更多