首页 > 解决方案 > Plotly Dash children= clause

问题描述

I'm using dash in Python and have seen the "children= " clause. However in most cases I can remove it and the code runs just the same! Nowhere in the documentation I can find the reason for including it.

From the documentation you get:

The children property is special. By convention, it's always the first attribute which means that you can omit it:

So what's the purpose of children= in the code if you can decide to not use it and everything runs fine?

标签: pythonplotly-dash

解决方案


有人可能想要包含它有几个可能的原因。来自 Python 之禅:

显式优于隐式。

另一个原因可能是您的组件具有要分配的多个不同属性,并且它更适合具有以下内容的样式:

html.Div(
    children='text',
    id='my-div',
    style=dict(color='green')
)

这也有助于语法突出显示,然后使子项与其他属性的颜色相匹配。否则,它可能看起来不一样,并且可能在一大段代码中变得更难看到。

使用 kwarg 还允许您将children属性作为第一个属性以外的东西传递。我不确定是否有必要这样做,但只有通过显式分配才有可能。


推荐阅读