python - 该函数如何使用 python 重新调整 Dash 中的正确参数?
问题描述
我有一个关于使用 python 在破折号中执行函数的问题。
这是我正在研究的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H6("Change the value in the text box to see callbacks in action!"),
html.Div(["Input: ",
dcc.Input(id='my-input', value='initial value', type='text')]),
html.Br(),
html.Div(id='my-output'),
])
@app.callback(
Output(component_id='my-output', component_property='children'),
Input(component_id='my-input', component_property='value')
)
def update_output_div(input_value):
return 'Output: {}'.format(input_value)
if __name__ == '__main__':
app.run_server(debug=True)
在回调之后,我们有一个函数“update_output_div”,和一个名为“input_value”的参数,但是代码怎么知道input_value是名为“my-input”的component_id的component_property?这个问题的第一个答案是因为顺序,但在这种情况下:
@app.callback(
[Output(component_id='plot1', component_property='children'),
Output(component_id='plot2', component_property='children'),
Output(component_id='plot3', component_property='children'),
Output(component_id='plot4', component_property='children'),
Output(component_id='plot5', component_property='children')],
[Input(component_id='input-type', component_property='value'),
Input(component_id='input-year', component_property='value')],
# REVIEW4: Holding output state till user enters all the form information. In this case, it will be chart type and year
[State("plot1", 'children'), State("plot2", "children"),
State("plot3", "children"), State("plot4", "children"),
State("plot5", "children")])
# Add computation to callback function and return graph
def get_graph(chart, year, children1, children2, c3, c4, c5):
?
解决方案
它尊重订单:
第一个 Input component_property 将作为函数的第一个参数,例如:
@app.callback(
Output(component_id='my-output', component_property='children'),
Input(component_id='my-input1', component_property='value1'),
Input(component_id='my-input2', component_property='value2'),
Input(component_id='my-input3', component_property='value3'),
State(component_id='my-input4', component_property='value4'),
State(component_id='my-input5', component_property='value5')
)
def update_output_div(value1, value2, value3, value4, value5):
return func(value1, value2, value3, value4, value5)
推荐阅读
- c# - 如何使用 NPOI 在 docx 文档的开头插入表格
- html - 如何使用 CSS 在表单周围添加边框?
- c++ - BEGIN_MESSAGE_MAP 错误“非静态成员引用必须相对于特定对象”MFC
- css - Internet Explorer IE 上 Font Awesome 面临的问题
- xcode - 使用 Picker View 时 UITest 中断
- firebase - google-services.json 文件中存储了什么样的数据(请详细讨论)
- php - 如何在 PHP 的 eval() 中将条件答案分配给变量?
- python - 过滤掉所有假矩阵的最快方法
- go - 我需要将 gopkg.lock 添加到 Git 吗?
- mongodb - Mac 上的 Strapi 开始安装问题