CKA*_*CKA 5 python plotly-dash
我对“孩子”在 Dash html 中所做的事情感到非常困惑。为什么它存在?你为什么要使用它?我尝试阅读文档,但没有多大帮助。
参考下面的代码块:
代码块:
app.layout = html.Div(children=[
# TODO1: Add title to the dashboard
html.H1("Airline Dashboard by CK", style = {'text-align':'center'}),
# REVIEW2: Dropdown creation
# Create an outer division
html.Div([
# Add an division
html.Div([
# Create an division for adding dropdown helper text for report type
html.Div(
[
html.H2('Report Type:', style={'margin-right': '2em'}),
]
),
# TODO2: Add a dropdown
dcc.Dropdown(id = 'input-type',
options = [{'label':'Yearly Airline Performance Report', 'value': 'OPT1'},
{'label':'Yearly Average Flight Delay Statistics', 'value': 'OPT2'}],
multi = False,
placeholder = 'Select a Report Type',
style={'width': '80%', 'padding': '3px', 'font-size': '20px', 'text-align-last': 'center'}
)
# Place them next to each other using the division style
], style={'display': 'flex'}),
# Add next division
html.Div([
# Create an division for adding dropdown helper text for choosing year
html.Div(
[
html.H2('Choose Year:', style={'margin-right': '2em'})
]
),
dcc.Dropdown(id='input-year',
# Update dropdown values using list comphrehension
options=[{'label': i, 'value': i} for i in year_list],
placeholder="Select a year",
style={'width': '80%', 'padding': '3px', 'font-size': '20px', 'text-align-last': 'center'}),
# Place them next to each other using the division style
], style={'display': 'flex'}),
]),
# Add Computed graphs
# REVIEW3: Observe how we add an empty division and providing an id that will be updated during callback
html.Div([], id='plot1'),
html.Div([
html.Div([], id='plot2'),
html.Div([], id='plot3')
], style={'display': 'flex'}),
# TODO3: Add a division with two empty divisions inside. See above disvision for example.
html.Div([
html.Div([], id='plot4'),
html.Div([], id='plot5')
], style = {'display':'flex'})
])
# Callback function definition
# TODO4: Add 5 ouput components
@app.callback(
[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
[Output("plot1", 'children'), Output("plot2", "children"),
Output("plot3", "children"), Output("plot4", "children"),
Output("plot5", "children")
])
Run Code Online (Sandbox Code Playgroud)
从文档的此页面:
- 儿童财产很特别。按照惯例,它始终是第一个属性,这意味着您可以省略它:html.H1(children='Hello Dash') 与 html.H1('Hello Dash') 相同。此外,它还可以包含字符串、数字、单个组件或组件列表。
某些组件(例如html.Div和html.P)接受其 prop 的值children。其他的,如dcc.Graph或dcc.Dropdown没有,需要其他道具才能正常运行。
正如 @KarlKnechtel 在他的评论中提到的,当一个组件是另一个组件的子组件时,它表示第一个组件嵌套在另一个组件中。以下是类似的:
在达世币中:
html.Div(
children=[
html.H1('This is some text'),
html.P('This is also some text'),
]
)
Run Code Online (Sandbox Code Playgroud)
在 HTML 中:
<div>
<h1>This is some text</h1>
<p>This is also some text</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望这能回答您的问题。
编辑:
在style其后添加将允许您更改 的样式,这可能会影响嵌套在其中的组件的样式,但这不是该 prop 的目的。正如文档提到的,您可以显式设置任何内容,也可以在第一个中传递相同的值,没有显式的关键字参数,Dash 会将其视为 prop 。不管怎样,在幕后,组件仍然接收其属性的值。childrenhtml.DivDivchildrenchildren=childrenchildren
该属性的目的children是允许用户嵌套组件,就像我们在原始 HTML 中所做的那样。如果没有该children道具,就不可能通过将相关项目包含在同一父元素中来将它们分组在一起(例如,将导航项目放在顶部导航栏中)。