【发布时间】:2021-02-23 14:42:08
【问题描述】:
我想在我的应用程序中为“年龄”字段插入一个下拉菜单,从 0 到 100。但是,有没有比编写一个长字典来定义下拉组件的值更好的方法? 如何避免 {'label': '1', 'value': '1'}, {'label': '2', 'value': '2'} ...最多100?
import dash
import dash_html_components as html
import dash_core_components as dcc
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Dropdown(
id='demo-dropdown',
options=[
{'label': '1', 'value': '1'},
{'label': '2', 'value': '2'},
{'label': '3', 'value': '3'}
],
value='NYC'
),
html.Div(id='dd-output-container')
])
@app.callback(
dash.dependencies.Output('dd-output-container', 'children'),
[dash.dependencies.Input('demo-dropdown', 'value')])
def update_output(value):
return 'Your age is "{}"'.format(value)
if __name__ == '__main__':
app.run_server(debug=True)
【问题讨论】:
标签: python drop-down-menu dropdown plotly-dash