【发布时间】:2019-09-02 00:54:24
【问题描述】:
您好,我正在开发一个 dash 应用程序,但不知何故,我无法更新不同 div 回调之间的图表。这就是应用程序的工作方式:
- 用户在前端的文本输入框中输入url,点击按钮运行分析。
- 用户也可以在运行分析之前选择查看模式,查看模式取决于下拉选择器
- url用于显示视频帧和运行python函数。
- 处理后的结果应该存储在 dcc.store 组件中。
- 然后调用存储的数据来更新图表。
以下是回调代码:
#Video Selection
@app.callback(Output("video-display", "url"),
[Input("submit_button", "n_clicks")],
[State('video_url', 'value')])
def select_footage(n_clicks, video_url):
if n_clicks is not None and n_clicks > 0:
url = video_url
return url
# Processing and Storing the results in dcc.store
@app.callback(Output("intermediate-value", "data"),
[Input("submit_button", "n_clicks")],
[State('video_url', 'value')])
def video_processing(n_clicks, value ):
global frame
if n_clicks is not None and n_clicks > 0:
frame = python_func(url)
return frame.to_json(orient='split')
# Callback to change the graph view mode div
@app.callback(Output("div-graph-mode", "children"),
[Input("dropdown-graph-view-mode", "value")])
def update_graph_mode(value):
if value == "graphical":
return [
html.Div(
children=[
html.P(children="Retention Score of Detected Labels",
className='plot-title'),
dcc.Graph(
id="bar-score-graph",
style={'height': '55vh', 'width': '100%'}
),
html.P(children="Audience Retention Behavior",
className='plot-title'),
dcc.Graph(
id="line_chart_retention",
style={'height': '45vh', 'width': '100%'}
)
]
)
]
else:
return []
@app.callback(Output("div-table-mode", "children"),
[Input("dropdown-graph-view-mode", "value")])
def update_table_mode(dropdown_value):
if dropdown_value == "table":
return [
html.Div(
children=[
html.P(children="Retention By Label",
className='plot-title'),
html.Div([
table.DataTable(
id="label_retention",
)],
style={'height': '45vh'}),
html.P(children="Retention by Time Stamp",
className='plot-title'),
html.Div([
table.DataTable(
id="timestamp_retention",
style_table={'maxHeight': '40vh', 'width': '100%', 'overflowY': 'scroll'})],
style={'height': '40vh'}
)
]
)
]
else:
return []
# Updating Graphs
@app.callback(Output("label_retention", "figure"),
[Input("dropdown-graph-view-mode", "value")])
def update_table_bar(value):
global frame
if frame is not None:
print(frame)
print("table")
print(value)
@app.callback(Output("bar-score-graph", "figure"),
[Input("dropdown-graph-view-mode", "value")])
def update_score_bar(value):
global frame
if frame is not None:
print(frame)
print("graph")
print(value)
现在发生的情况是,如果我尝试在两种图形视图模式之间切换,应用程序不会反映图形,需要再次单击按钮才能获得结果。所以,基本上我相信当我切换下拉菜单时,数据不会在 dcc.store 组件中丢失。
如何使应用程序的行为方式使我的 python 函数仅在提交按钮上运行一次,但随后我能够在视图模式之间切换以查看图表。
提前非常感谢!!
附:这只是一个sn-p代码,因为代码太长了,但是如果您想查看整个代码,请告诉我。
更新:我刚刚意识到,当我选择图表模式时,应用程序会打印表格模式的结果,当我选择表格模型时,应用程序会打印图表模式的结果。我无法弄清楚为什么会这样。
【问题讨论】:
标签: python callback plotly-dash