【问题标题】:Plotly-Dash: Hide and Unhide DropdownPlotly-Dash:隐藏和取消隐藏下拉菜单
【发布时间】:2021-05-22 21:43:14
【问题描述】:

我想用我的下拉菜单隐藏启动我的 Dash,然后在另一个下拉菜单上选择某些内容后取消隐藏我的第一个下拉菜单。如果您有任何想法,我想知道,也许这是我的小脚本上的一个错误。

这是一个小例子:

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

app = dash.Dash('example')

app.layout = html.Div([
    dcc.Dropdown(
        id = 'dropdown-to-show_or_hide-element',
        options=[
            {'label': 'Show element', 'value': 'on'},
            {'label': 'Hide element', 'value': 'off'}
        ],
        value = 'off'
    ),

    # Create Div to place a conditionally visible element inside
    html.Div([
        # Create element to hide/show, in this case an 'Input Component'
        dcc.Input(
        id = 'element-to-hide',
        placeholder = 'something',
        value = 'Can you see me?',
        )
    ], style= {'display': 'none'} # <-- This is the line that will be changed by the dropdown callback
    )
    ])

@app.callback(
   Output(component_id='element-to-hide', component_property='style'),
   [Input(component_id='dropdown-to-show_or_hide-element', component_property='value')])

def show_hide_element(visibility_state):
    if visibility_state == 'on':
        return {'display': 'block'}
    if visibility_state == 'off':
        return {'display': 'none'}

if __name__ == '__main__':
    app.server.run(debug=False, threaded=True)

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    通过稍微更改您的代码(我更改了您的函数采用的参数并在 if 语句中使用相同的参数,此代码似乎可以工作)。我还将原始样式更改为block,它允许元素存在,但仅在show 下拉列表中显示。

    import dash
    from dash.dependencies import Input, Output
    import dash_core_components as dcc
    import dash_html_components as html
    
    app = dash.Dash('example')
    
    app.layout = html.Div([
        dcc.Dropdown(
            id = 'dropdown-to-show_or_hide-element',
            options=[
                {'label': 'Show element', 'value': 'on'},
                {'label': 'Hide element', 'value': 'off'}
            ],
            value = 'off'
        ),
    
        # Create Div to place a conditionally visible element inside
        html.Div([
            # Create element to hide/show, in this case an 'Input Component'
            dcc.Input(
            id = 'element-to-hide',
            placeholder = 'something',
            value = 'Can you see me?',
            )
        ], style= {'display': 'block'} # <-- This is the line that will be changed by the dropdown callback
        )
        ])
    
    @app.callback(
       Output(component_id='element-to-hide', component_property='style'),
       [Input(component_id='dropdown-to-show_or_hide-element', component_property='value')])
    
    def show_hide_element(value):
        if value == 'on':
            return {'display': 'block'}
        if value == 'off':
            return {'display': 'none'}
    
    if __name__ == '__main__':
        app.server.run(debug=False)
    

    【讨论】:

    • 您好,谢谢!最后,我也找到了解决方案。如果你在 dcc.Input 中删除 .style= {'display': 'block'} ,效果会很好。老实说,我不是很清楚,为什么它会在这个参数中起作用
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 2018-08-18
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多