Ива*_*ков 21 python dashboard plotly plotly-dash
我需要隐藏一些组件,例如通过单击复选框(例如,图形或表格)。但是,文档没有为此提供合适的部分。提前致谢!
Tim*_*sen 27
您可以将需要隐藏的组件放置在 an 中,html.div([])并在回调中将其“显示”选项更改为“无”。回调应该有例如 Dropdown 作为Input和 Component 在html.div([])as Output 中。
以下是一个仅包含一个下拉列表和一个输入组件的 Web 应用程序,该组件根据下拉列表的值可见/隐藏。复制时它应该直接工作:
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 = 'on'
),
# 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(visibility_state):
if visibility_state == 'on':
return {'display': 'block'}
if visibility_state == 'off':
return {'display': 'none'}
if __name__ == '__main__':
app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)
请注意,如果在 内放置了多个组件html.div([]),回调仍然只会更改它输出的组件的“显示”属性。因此,您可以将其他 Dash 组件放置在同一个 Div 中,而不会影响它们的可见性。
希望这能正确回答您的问题。
自从两年前给出这个答案以来,Dash 项目和用户文档已经有了很大的发展。文档现在展示了在回调之间实现状态共享的多种方法,第一种方法是将数据存储在隐藏的 div 中,如该答案所示。
请参阅此处文档中的特定页面。