我正在使用plotly dash创建一个Web应用程序,用于图形生成和回调处理。我使用dash-core-components v0.18.1
尝试动态添加图表时(每次单击按钮时,都会添加新图表),控制台中会出现错误:
bundle.js?v = 0.11.2:9 Uncaught(in promise)错误:dash_core_components 没找到。在at.asolve(bundle.js?v = 0.11.2:9)at s (bundle.js?v = 0.11.2:9)在s(bundle.js?v = 0.11.2:9)的Array.map()处 位于s的Array.map()处的s(bundle.js?v = 0.11.2:9)处的Array.map() (bundle.js?v = 0.11.2:9)at e.value(bundle.js?v = 0.11.2:9)at at p._renderValidatedComponentWithoutOwnerOrContext (react-dom@15.4.2.min.js V = 0.11.2:13)
我只有一个带有按钮的空白页面和点击回调。 回调只是用图表填充div。
布局代码:
Broadcast
回拨代码
import numpy as np
import cv2
import sys
from imutils.object_detection import non_max_suppression
cap = cv2.VideoCapture("video.mp4")
hog_descriptor = cv2.HOGDescriptor()
hog_descriptor.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
tracker = cv2.TrackerKCF_create()
ret, frame = cap.read()
while (1):
ret, frame = cap.read()
(rects, weights) = hog_descriptor.detectMultiScale(frame, winStride=(16, 16),
padding=(8, 8), scale=1.05)
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
for (xA, yA, xB, yB) in rects:
cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
ret = tracker.init(frame)
ret, = tracker.update(frame)
cv2.imshow("video", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
奇怪的是,如果在加载页面时存在另一个图形,那么一切正常。这是Dash中的错误吗?
答案 0 :(得分:3)
事实证明,这是Dash的一个缺陷。
解决方法:在加载时创建隐藏图形,如下所示:
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
原因是所需的库仅在页面加载时加载,因此如果在加载时布局中不存在图形,则不会加载某些虚线表库。