gpt4 book ai didi

python - Plotly Dash 无法动态创建图形

转载 作者:太空狗 更新时间:2023-10-30 01:11:39 25 4
gpt4 key购买 nike

我正在使用 plotly dash 创建一个网络应用程序来生成图形和处理回调。我使用 dash-core-components v0.18.1

当尝试动态添加图形时(每次单击按钮,都会添加一个新图形),控制台中出现错误:

bundle.js?v=0.11.2:9 Uncaught (in promise) Error: dash_core_components was not found. at Object.resolve (bundle.js?v=0.11.2:9) at s (bundle.js?v=0.11.2:9) at Array.map () at s (bundle.js?v=0.11.2:9) at Array.map () at s (bundle.js?v=0.11.2:9) at Array.map () at s (bundle.js?v=0.11.2:9) at e.value (bundle.js?v=0.11.2:9) at p._renderValidatedComponentWithoutOwnerOrContext (react-dom@15.4.2.min.js?v=0.11.2:13)

我只有一个空白页面,其中包含一个按钮和一个点击回调。回调只是用图表填充一个 div。

布局代码:

class ContentController(object):

graph_names = None

def __init__(self):
self.graph_names = []
# UNCOMMENT THIS LINE TO MAKE IT WORK
# self.graph_names = ["start_graph"]

def layout(self):
return html.Div([html.Button("Add Graph", id='button_id'),
html.Div(self.graphics_layout, id='graphics_div')],
id='main_div')

@property
def graphics_layout(self):
graphs = []
for name in self.graph_names:
graph = self.create_graph()
graphs.append(dcc.Graph(id=name, figure=graph, style= {'width': '600px', 'height': '300px'}))

return graphs

def create_graph(self):
graph_data = {
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
return graph_data

回调代码

@app.callback(
Output(component_id='graphics_div', component_property='children'),
[Input(component_id='button_id', component_property='n_clicks')]
)
def callback_function(n_clicks):
print(n_clicks)
if n_clicks is None:
return ""

name = "graph_" + str(n_clicks)
content.graph_names.append(name)
return content.graphics_layout

奇怪的是,如果加载页面时出现另一个图表,那么一切正常。这是 Dash 中的错误吗?

最佳答案

事实证明,这是 Dash 的一个缺陷。

解决方法:像这样在加载时创建隐藏图:

app.layout = html.Div([
html.Div(id='content'),
dcc.Location(id='location', refresh=False),
html.Div(dt.DataTable(rows=[{}]), style={‘display’: ‘none’})
])

原因是所需的库仅在页面加载时加载,因此如果在加载时布局中不存在图形,则不会加载某些破折号库。

更多信息可以在 dash 问题板上找到: HereHere

关于python - Plotly Dash 无法动态创建图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48886533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com