【问题标题】:insertion of list to a plotly dash dropdown resulting in empty dropdown将列表插入到绘图的破折号下拉列表中,导致下拉列表为空
【发布时间】:2022-07-01 22:27:13
【问题描述】:

我一直在观看各种使用 dash 核心组件创建下拉菜单的教程。我能够重新创建youtube tutorial 中演示的那个。我对该工作代码的唯一更改是我将options 参数中的字典替换为我希望用户能够从中选择的物种列表。我可以接受标签与所有物种的值相同,所以我不认为我需要dash plotly dropdown documentation 中的字典。

我无法在options 参数中插入列表是否有原因?是变量名还是实际列表本身?

预期结果: 插入到工作代码中的列表会创建一个包含列表所有元素的下拉列表

实际结果: 下拉菜单创建没有错误,但为空

这是不产生下拉值的代码:

app.layout = html.Div([
    
    dcc.Dropdown(
        id = 'first-dropdown',
        options = [species],                #where species = long list of strings
        value = 'Pacific Water Shrew'       #one of the strings contained in species
    )
])

if __name__ =='__main__':
    app.run_server()

【问题讨论】:

  • species 已经是列表了吗?如果是,您正在通过执行 [species] 创建列表列表 尝试执行 options = species
  • 啊,这是一个好点,我没有意识到这一点。我会试一试。但是我想我应该在我的问题中提到我也尝试过以真实形式插入物种:options = ['Pacifiic Water Shrew','Whihtebark Pine',......] 并且产生了相同的结果。

标签: python plotly dropdown


【解决方案1】:

根据评论,如果 species 是一个列表,[species] 是一个列表列表。只需通过列表即可。还要注意拼写错误,评论中列表中的值与 value 参数不一致。

from dash import html, dcc
import dash

# Build App
# app = JupyterDash(__name__)
app = dash.Dash()
species = ['Pacific Water Shrew','Whihtebark Pine']
app.layout = html.Div(
    [
        dcc.Dropdown(
            id="first-dropdown",
            options=species,  # where species = long list of strings
            value="Pacific Water Shrew",  # one of the strings contained in species
        ),
        html.Pre(dash.__version__)
    ]
)

app.run_server()

【讨论】:

  • 感谢您的回复。您的代码(完全相同,但使用 Dash 而不是 JupyterDash 并且没有 mode 参数,因为它会产生错误)产生一个下拉菜单。但是,和以前一样,该下拉菜单是空的,而不是产生选项 [species]。
  • 修改为使用破折号,它可以工作。还添加了一个 Pre 来显示 2.3.0 的破折号版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多