简体   繁体   English

Python 破折号下拉占位符文本

[英]Python dash dropdown placeholder text

I am trying to build a dropdown menu in dash, where the text of the placeholder should be defined by another callback.我正在尝试在破折号中构建一个下拉菜单,其中占位符的文本应由另一个回调定义。

What I tried so far is to have the placeholder defined by a html.Div, like this: dcc.Dropdown(id = 'test_dropdown', options = [{'label': 'Test1', 'value': '1'},{'label': 'Test2', 'value': '2'}], placeholder = html.Div(id = 'test_placeholder')) , with the text being defined by the other callback.到目前为止,我尝试的是让占位符由 html.Div 定义,如下所示: dcc.Dropdown(id = 'test_dropdown', options = [{'label': 'Test1', 'value': '1'},{'label': 'Test2', 'value': '2'}], placeholder = html.Div(id = 'test_placeholder')) ,文本由另一个回调定义。 This resulted in the following error message: Invalid argument placeholder passed into Dropdown with ID "test_dropdown".这导致以下错误消息:无效的参数placeholder传递到 ID 为“test_dropdown”的下拉菜单。 Expected string .预期的string Was supplied type array .提供类型array

If I remove the square brackets around the html.Div, the error message becomes: The dash_core_components.Dropdown component (version 1.16.0) with the ID "test_dropdown" detected a Component for a prop other than children Did you forget to wrap multiple children in an array?如果我删除 html.Div 周围的方括号,错误消息将变为: ID 为“test_dropdown”的dash_core_components.Dropdown组件(版本 1.16.0)检测到除children项以外的道具的组件您是否忘记包装多个children项在一个数组中? Prop placeholder has value Div(id='test_placeholder')道具占位符具有值 Div(id='test_placeholder')

Has anyone got suggestions on how I could implement a placeholder that depends on another dropdown?有没有人对我如何实现依赖于另一个下拉列表的占位符提出建议? Or would it be better to implement multiple dropdowns, and hide all but one using a hidden html.Div?还是使用隐藏的 html.Div 实现多个下拉菜单并隐藏除一个之外的所有下拉菜单会更好吗?

Thanks in advance for your help在此先感谢您的帮助

Simply put the placeholder of the dcc.Dropdown as Output of your callback.只需将Outputplaceholder设置为回调的dcc.Dropdown即可。

Here is a minimal example with a dcc.Input that controls the placeholder .这是一个控制placeholderdcc.Input的最小示例。 Of course, anything can be used as Input for the callback that changes the placeholder , eg the value of another dcc.Dropdown :当然,任何东西都可以用作改变placeholder的回调的Input ,例如另一个dcc.Dropdown的值:

import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output

app = dash.Dash(__name__)
app.layout = html.Div([
    dcc.Input(id='demo-input', value='Type here'),
    dcc.Dropdown(
        id='demo-dropdown',
        options=[
            {'label': 'New York City', 'value': 'NYC'},
            {'label': 'Montreal', 'value': 'MTL'}
        ]
    )
])

@app.callback(
    Output('demo-dropdown', 'placeholder'), # <- dropdown placeholder gets updated
    Input('demo-input', 'value'))
def update_placeholder(value):
    return value

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM