为什么 Dash dcc.Dropdown() 只将所选菜单项的前 3 个字符返回给回调函数,而不是整个值?

问题描述 投票:0回答:0

以下代码在过去几周一直有效,但是,就在前几天,如下所示的

dcc.Dropdown
菜单开始出现奇怪的错误。每当用户在下拉菜单中选择鸡尾酒时,只有鸡尾酒的前 3 个字符返回给回调函数。

例如:

鉴于

cocktail_names
是格式正确的列表(例如:
['#106 Cocktail', '10 to 7', '1626', "1880's Americana", '19th Century']
)。

  • Case 1:如果用户选择了选项
    "1880's Americana"
    ,那么回调函数返回字符串
    "188"
    .
  • Case 2:如果用户选择了选项
    "10 to 7"
    ,那么回调函数返回字符串
    "10 "
    .
  • Case 3:如果用户选择了选项
    "#106 Cocktail'"
    ,那么回调函数返回字符串
    "#10"
    .
# import statements

import dash
import dash_bootstrap_components as dbc
from dash import Input, Output, State, dcc, html, callback
import dash_html_components as html
import pandas as pd
import os
import pymysql.cursors

# connect dahs page to main app
dash.register_page(__name__, path='/Search')

# dropdown menu selection items
# cocktail_names is a list of cocktails, for example: ['#106 Cocktail', '10 to 7', '1626', "1880's Americana", '19th Century']
cocktail_names.sort()
options = [{'label': name, 'value': name} for name in cocktail_names]

# dash layout

layout = html.Div(children=[
# Cocktail Relay System
    html.Div([
        dcc.Dropdown(options=options,
        placeholder = "Select or Search for Cocktail",
        value= None,  # initial value displayed when page first loads
        clearable=False,
        searchable = True,
        style={"backgroundColor": "white", "color": "black"},
        id = "dropdown_cocktail"),
    ]),
    html.Br(),
    html.H1(id = "Cocktail_Output", style={'textAlign': 'center', 'margin-bottom':25, 'margin-top':25}),

])

# callback function

@callback(
Output("Cocktail_Output", component_property = "children"),
Input("dropdown_cocktail", component_property = "value")
)
def cocktail_output(user_input):
    output = ""
    if user_input is None:
        output = None
    else:
        return user_input
    return output

我通过确保

cocktails_names
列表正确包含所有标签来进行一些错误测试,它确实如此。这意味着此错误必须来自下拉菜单和回调函数之间的通信。如果有人对可能导致此奇怪错误的原因有任何建议,请告诉我。

python drop-down-menu callback plotly-dash
© www.soinside.com 2019 - 2024. All rights reserved.