我想创建一个下拉仪表板,用户可以在其中从列中选择一个值。最后的输出是另一列中的值。
例如,如果我从Col_1中选择“低延迟”,从Col_2中选择“高峰月”,从Col_3中选择“高峰时间”,从Col_4中选择“ 90”,那么最终输出应显示Final_1和Final_2中的相应值,即“ 3” ”和“ 44”。
我尝试在破折号python上执行此操作。我一直在看以下链接:-“ https://community.plot.ly/t/dynamic-controls-and-dynamic-output-components/5519”
Sample df on which I am performing code
这是我的代码,因为我在传递值时遇到麻烦,所以我一直在手动输入值。
app = dash.Dash()
app.layout = html.Div([
dcc.Dropdown(
id='datasource-1',
options=[
{'label': i, 'value': i} for i in ['Low Delay','High Delay']
],
),
dcc.Dropdown(
id='datasource-2',
options=[
{'label': i, 'value': i} for i in ['Peak Month','Non Peak Month']
]
),
dcc.Dropdown(
id='datasource-3',
options=[
{'label': i, 'value': i} for i in ['Peak Hour', 'Non Peak Hour']
]
),
dcc.Dropdown(
id='datasource-4',
options=[
{'label': i, 'value': i} for i in ['60','90','120','150','>180']
]
),
html.Hr(),
html.Div('Select Hour and Threshold Delay'),
html.Div(
id='controls-container'
),
html.Hr(),
html.Div('Output'),
html.Div(
id='output-container'
)
])
def generate_control_id(value):
return 'Control {}'.format(value)
DYNAMIC_CONTROLS = {
'Low Delay': dcc.Dropdown(
id=generate_control_id('Low Delay'),
options=[{'label': '{}'.format(i), 'value': i} for i in ['Peak Hour', 'Non Peak Hour']]
),
'High Delay': dcc.Dropdown(
id=generate_control_id('High Delay'),
options=[{'label': '{}'.format(i), 'value': i} for i in ['Peak Hour', 'Non Peak Hour']]
),
'Peak Month': dcc.Dropdown(
id=generate_control_id('X'),
options=[{'label': '{}'.format(i), 'value': i} for i in ['60','90','120','150','>180']]
),
'Non Peak Month': dcc.Dropdown(
id=generate_control_id('Y'),
options=[{'label': 'Dropdown Y: {}'.format(i), 'value': i} for i in ['60','90','120','150','>180']]
)
}
@app.callback(
Output('controls-container', 'children'),
[Input('datasource-1', 'value'),
Input('datasource-2', 'value')])
def display_controls(datasource_1_value, datasource_2_value):
# generate 2 dynamic controls based off of the datasource selections
return html.Div([
DYNAMIC_CONTROLS[datasource_1_value],
DYNAMIC_CONTROLS[datasource_2_value],
])
def generate_output_id(value1, value2):
return '{} {} container'.format(value1, value2)
@app.callback(
Output('output-container', 'children'),
[Input('datasource-1', 'value'),
Input('datasource-2', 'value')])
def display_controls(datasource_1_value, datasource_2_value):
# create a unique output container for each pair of dyanmic controls
return html.Div(id=generate_output_id(
datasource_1_value,
datasource_2_value
))
def generate_output_callback(datasource_1_value, datasource_2_value):
def output_callback(control_1_value, control_2_value):
# This function can display different outputs depending on
# the values of the dynamic controls
return '''
You have selected {} and {} which were
generated from {} (datasource 1) and and {} (datasource 2)
'''.format(
control_1_value,
control_2_value,
datasource_1_value,
datasource_2_value
)
return output_callback
app.config.supress_callback_exceptions = True
for value1, value2 in itertools.product(
[o['value'] for o in app.layout['datasource-1'].options],
[o['value'] for o in app.layout['datasource-2'].options]):
app.callback(
Output(generate_output_id(value1, value2), 'children'),
[Input(generate_control_id(value1), 'value'),
Input(generate_control_id(value2), 'value')])(
generate_output_callback(value1, value2)
)
if __name__ == '__main__':
app.run_server()
我想要我的数据框上的预期输出。 如果我从Col_1中选择“低延迟”,从Col_2中选择“高峰月”,从Col_3中选择“高峰时间”,从Col_4中选择“ 90”,那么最终输出应显示Final_1和Final_2中的相应值,分别是“ 3”和“ 44” < / p>