【问题标题】:How the function can regonize the correct parameter in Dash using python?该函数如何使用 python 识别 Dash 中的正确参数?
【发布时间】:2021-07-30 14:46:31
【问题描述】:

我有一个关于使用 python 在 dash 中执行函数的问题。

这是我正在研究的代码:

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):

?

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    它尊重命令:

    第一个 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)
    

    【讨论】:

    • 我认为这可能是因为订单,但在我的问题中看到我的版本,请你再帮忙吗?
    • component_property 'value' 是不同组件的公共属性(例如,就像子组件一样) - 您可以有来自不同组件的 N 个不同输入,所有这些都输入它们的 component_property='value'。按照你的回调语句的顺序:你的第一个函数参数'chart'从component_id ='input_type'接收'value'输入;您的第二个函数参数“year”从 component_id = 'input_year' 接收“value”输入......对于州也是如此
    猜你喜欢
    • 2019-08-02
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多