简体   繁体   English

破折号输入,Output 组件属性

[英]Dash Input, Output Component Property

Referring to documentation link as below:-参考文档链接如下:-

dash-plotly破折号

In the documentation, component_property for the Input & Output callback is explained that:-在文档中, Input & Output callbackcomponent_property解释为:-

In Dash, the inputs and outputs of our application are simply the properties of a particular component.在 Dash 中,我们应用程序的输入和输出只是特定组件的属性。 In this example, our input is the "value" property of the component that has the ID "my-input".在本例中,我们的输入是 ID 为“my-input”的组件的“value”属性。 Our output is the "children" property of the component with the ID "my-output我们的 output 是 ID 为“my-output”的组件的“children”属性

However in example code copied below, I can't find the component_property of value or children in the html skeleton.但是在下面复制的示例代码中,我在 html 骨架中找不到valuechildrencomponent_property It is noted from the documentation that the children is omitted.从文档中注意到, children被省略了。

Q: how do I know if I have to specify value , children or some other component_property ?问:我怎么知道我是否必须指定valuechildren或其他一些component_property

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)

If you want to know whether you have to explicitly give a specific component_property when using a component, you will have to look at the documentation of that component.如果您想知道在使用component_property时是否必须显式给出特定的 component_property,则必须查看该组件的文档。 It should tell you which properties are mandatory and which are optional.它应该告诉您哪些属性是强制性的,哪些是可选的。

For most components, the majority of properties is optional.对于大多数组件,大多数属性是可选的。 Look at the documentation for html.Div here for example.例如,在此处查看html.Div的文档。 Sadly, this example also shows that the documentation is not always complete on this, as the properties n_clicks and n_clicks_timestamp are also optional but not listed so as of now.遗憾的是,此示例还表明文档并不总是完整的,因为属性n_clicksn_clicks_timestamp也是可选的,但目前尚未列出。 That makes all properties of the html.Div optional.这使得html.Div的所有属性都是可选的。

In practice, you will mostly explicitly list only the properties that you want to set to some initial value.在实践中,您通常只会显式列出要设置为某个初始值的属性。 If you should be missing any mandatory ones, Dash will throw an Exception.如果您遗漏了任何强制性的,Dash 将抛出异常。

Note that you can use any property as Input / Output / State of a callback, regardless of whether they are optional or not.请注意,您可以使用任何属性作为回调的Input / Output / State ,无论它们是否可选。 They will exist with some default value even if you did not name them explicitly.即使您没有明确命名它们,它们也会以一些默认值存在。

Regarding the example you gave, the component_property value is actually explicitly given in this line, you might have overlooked it:关于您给出的示例, component_property value实际上是在这一行中明确给出的,您可能忽略了它:

dcc.Input(id='my-input', value='initial value', type='text')]),

It's the second argument to dcc.Input .这是dcc.Input的第二个参数。 The children property of the Output component is indeed not explicitly given in the layout. Output组件的children属性确实没有在布局中明确给出。 I added two working alternatives that include it below:我添加了两个可行的替代方案,其中包括以下内容:

html.Div(id='my-output'),                # from the given example
# html.Div(children=[], id='my-output'), # explicitly give children property
# html.Div([], id='my-output'),          # name of the children argument may be dropped if it goes first

Note how the third way is also the one used in the other two html.Div s in your example.请注意,在您的示例中,第三种方式也是其他两个html.Div中使用的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM