Python 包含多个值的 Dash 下拉列表
Python Dash dropdown with many values
我想在我的应用程序中为从 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)
你可以有一个列表理解来生成字典列表,如下所示:
options=[{"label":str(i),"value":str(i)} for i in range(0,101)]
我想在我的应用程序中为从 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)
你可以有一个列表理解来生成字典列表,如下所示:
options=[{"label":str(i),"value":str(i)} for i in range(0,101)]