dash Dropdown 根据选择的数量提供字符串或列表

dash Dropdown provides either string or list depending on number of selections

我目前正在使用 dash 开发股票分析工具。我有一个下拉菜单,其中包含纳斯达克 100 种符号,我试图将其变成 return 折线图,每个符号都有一条线 selected.

对于下拉列表,如果我有一个符号 selected,则 returned 值是一个字符串,如果我 select 多个,那么它就是一个列表。

我正在尝试使用回调,例如:

@app.callback(
    Output(component_id='stock-graph-line', component_property='figure'),
    Input(component_id='stock-input', component_property='value'),
    suppress_callback_exceptions = True
)
def update_stock_symbol(input_value):
    for i in input_value:
        fig.append_trace({'x':df.index,'y':df[([i], 'close')], 'type':'scatter','name':'Price [Close]'},1,1)   

    fig['layout'].update(height=1000, title=input_value, template="plotly_dark")
    return fig

但是,for 循环不适用于仅一个符号 selected,因为它获取的是一个字符串,而不是一个列表。 dash 中是否有指定 return 回调类型的选项? (我可以强制它把一个符号作为列表项传递吗?)还是必须用测试类型的 if 语句来处理?

我设置了一个下拉列表,它总是返回 None 或一个列表:

dcc.Dropdown(
        id='dropdown-id',
        options=[
            {'label': 'a', 'value': 'a'},
            {'label': 'b', 'value': 'b'},
            {'label': 'c', 'value': 'c'},
            {'label': 'd', 'value': 'd'},
            {'label': 'e', 'value': 'e'},
        ],
        multi=True
    ),

也许你的设置不同。如果那行不通,那么就这样检查:

def update_stock_symbol(input_value):
    if isinstance(input_value, str):
        input_value = [input_value]
    for i in input_value:
        ...