使用破折号创建仪表板时回调错误

Callback error when creating a dashboard using dash

我正在尝试使用破折号构建交互式仪表板。我想显示一个散点图,每个图代表一个战士,y 轴表示赢得的战斗次数,x 轴表示战斗的次数。

我想允许用户使用多下拉箭头 select 不同的分区(到 select 一个或多个分区)。

借助一些在线帮助,这是我想出的:

data['bouts_fought'] = data['w'].astype('float')+data['l'].astype('float')+data['d'].astype('float')
WEIGHT_CLASS = data['division'].unique()
app = dash.Dash()
app.css.append_css({
    "external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
# layout
app.layout = html.Div(children=[
    html.H1(children='Visualizing boxer stats', style={
        'textAlign': 'center',
    }),
    dcc.Dropdown(
        id='weight_class',
        options=[{'label': i, 'value': i} for i in data['division'].unique()],
        multi=True
    ),
    dcc.Graph(
        id='total-bouts-v-bouts-won',
    )
])
@app.callback(
    dash.dependencies.Output('total-bouts-v-bouts-won', 'figure'),
    [dash.dependencies.Input('weight_class', 'value')])
def update_scatterplot(weight_class):
    if weight_class is None or weight_class == []:
        weight_class = WEIGHT_CLASS

    weight_df = data[(data['division'].isin(weight_class))]
    return {
        'data': [
            go.Scatter(
                x=weight_df['bouts_fought'],
                y=weight_df['w'],
                text=weight_df['name'],
                mode='markers',
                opacity=0.5,
                marker={
                    'size': 14,
                    'line': {'width': 0.5, 'color': 'blue'}
                },
            )
        ],
        'layout': go.Layout(
            xaxis={'title': 'Bouts fought'},
            yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10},
            legend={'x': 0, 'y': 1},
            hovermode='closest'
        )
    }
if __name__ == '__main__':
    app.run_server(debug=True)

但是,当我尝试 运行 这个应用程序时,我收到了这些警告消息:

options[14].label in Dropdown with ID "weight_class" is required but it was not provided. Callback error updating

total-bouts-v-bouts-won.figure ValueError: Invalid properties specified for object of type plotly.graph_objs.layout.YAxis: ('1', 'b', 't', 'r')

这是我的数据示例:

{'name': {0: 'Roberto Salas',
  3: 'James Jackson',
  6: 'Alex Love',
  9: 'Juan Centeno',
  12: 'Jordan Weeks'},
 'division': {0: 'cruiser',
  3: 'heavy',
  6: 'bantam',
  9: 'fly',
  12: 'super middle'},
 'w': {0: 5.0, 3: 4.0, 6: 3.0, 9: 4.0, 12: 2.0},
 'l': {0: 0.0, 3: 0.0, 6: 0.0, 9: 3.0, 12: 0.0},
 'd': {0: 0.0, 3: 1.0, 6: 0.0, 9: 1.0, 12: 0.0},
 'location': {0: 'USA', 3: 'USA', 6: 'USA', 9: 'USA', 12: 'USA'},
 'from': {0: 2016.0, 3: 2017.0, 6: 2018.0, 9: 2016.0, 12: 2019.0},
 'sex': {0: 'male', 3: 'male', 6: 'female', 9: 'male', 12: 'male'}}

我根据给出的答案更新了我的代码,但仍然收到此错误消息:

Error: options[14].label in Dropdown with ID "weight_class" is required but it was not provided.

    at propTypeErrorHandler (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/dash_renderer.v1_2_0m1574163797.dev.js:33569:9)

    at CheckedComponent (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/dash_renderer.v1_2_0m1574163797.dev.js:30047:77)

    at renderWithHooks (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:13073:18)

    at mountIndeterminateComponent (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:15155:13)

    at beginWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:15760:16)

    at performUnitOfWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19447:12)

    at workLoop (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19487:24)

    at renderRoot (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19570:7)

    at performWorkOnRoot (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:20477:7)

    at performWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:20389:7)

错误

total-bouts-v-bouts-won.figure ValueError: Invalid properties specified for object of type plotly.graph_objs.layout.YAxis: ('1', 'b', 't', 'r')

是因为你的布局定义:

    'layout': go.Layout(
        xaxis={'title': 'Bouts fought'},
        yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10},
        legend={'x': 0, 'y': 1},
        hovermode='closest'
    )

不确定您要用 yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10} 做什么,但是 1btr 不是有效属性。您可以使用 print(go.Layout.yaxis.__doc__) 查看 yaxis 字典的完整有效属性列表。

如果我从您的布局字典中删除 yaxis,该应用程序将使用您的示例数据正常运行,并且当我在下拉列表中添加 select 项时图表会更新。我在任何时候都没有看到第一个错误(选项 [14].label in Dropdown ...)。