为什么我的DASH应用不使用放射性更新
在下面的DASH应用程序中,我的图形只有在更改'points_category'的值时才更新 - 如果我单击'legend_display'对象上的一个按钮,那么传奇不会出现或消失,直到我更改'points_category'中的值目的。
有人可以解释原因,以及当我更改任何一个值时如何更新应用程序?
谢谢! 蒂姆
import sys
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
tab_names = ['KnownClusterBlast', 'ClusterBlast']
app.layout = html.Div([
dcc.Tabs(id='choose_clusterblast',
value='ClusterBlast',
children=[
dcc.Tab(label='ClusterBlast', value='ClusterBlast'),
dcc.Tab(label='KnownClusterBlast', value='KnownClusterBlast')]),
dcc.RadioItems(options=[{'label': 'homolog_bgc_type',
'value': 'homolog_bgc_type'},
{'label': 'genus',
'value': 'genus'},
{'label': 'species',
'value': 'species'},
{'label': 'strain',
'value': 'strain'}],
value = 'genus',
id = 'points_category'),
dcc.RadioItems(options=[{'label': 'show_legend',
'value': 'show_legend'},
{'label': 'dont_show_legend',
'value': 'dont_show_legend'}],
value = 'dont_show_legend',
id = 'legend_display'),
dcc.Graph(id = 'scatter_graph')])
@app.callback(
[Output('scatter_graph', 'figure')],
[Input('choose_clusterblast', 'value'),
Input('points_category', 'value'),
Input('legend_display', 'value')]
)
def update_figure(type_clusterblast, category_colour, legend_display):
if type_clusterblast == 'KnownClusterBlast':
df = known_clusterblast_df
elif type_clusterblast == 'ClusterBlast':
df = clusterblast_df
else:
sys.exit('error')
fig = px.scatter(df,
x="homolog_percent",
y="multigeneblast_score",
color = category_colour,
range_x = [0, 100],
range_y = [0, max(df['multigeneblast_score']) * 1.1],
height = 750,
width = 2000)
if legend_display == 'dont_show_legend':
fig.update_layout(showlegend=False)
elif legend_display == 'show_legend':
fig.update_layout(showlegend=True)
else:
sys.exit()
fig.update_layout(transition_duration=500)
return [fig]
if __name__ == '__main__':
app.run_server(debug=True)
In the dash app below, my figure legend only updates when I change the values of 'points_category' - if I click a button on the 'legend_display' object then the legend does not appear or disappear until I change a value in the 'points_category' object.
Can someone explain why, and how to make the app update when I change either value?
Thanks!
Tim
import sys
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
tab_names = ['KnownClusterBlast', 'ClusterBlast']
app.layout = html.Div([
dcc.Tabs(id='choose_clusterblast',
value='ClusterBlast',
children=[
dcc.Tab(label='ClusterBlast', value='ClusterBlast'),
dcc.Tab(label='KnownClusterBlast', value='KnownClusterBlast')]),
dcc.RadioItems(options=[{'label': 'homolog_bgc_type',
'value': 'homolog_bgc_type'},
{'label': 'genus',
'value': 'genus'},
{'label': 'species',
'value': 'species'},
{'label': 'strain',
'value': 'strain'}],
value = 'genus',
id = 'points_category'),
dcc.RadioItems(options=[{'label': 'show_legend',
'value': 'show_legend'},
{'label': 'dont_show_legend',
'value': 'dont_show_legend'}],
value = 'dont_show_legend',
id = 'legend_display'),
dcc.Graph(id = 'scatter_graph')])
@app.callback(
[Output('scatter_graph', 'figure')],
[Input('choose_clusterblast', 'value'),
Input('points_category', 'value'),
Input('legend_display', 'value')]
)
def update_figure(type_clusterblast, category_colour, legend_display):
if type_clusterblast == 'KnownClusterBlast':
df = known_clusterblast_df
elif type_clusterblast == 'ClusterBlast':
df = clusterblast_df
else:
sys.exit('error')
fig = px.scatter(df,
x="homolog_percent",
y="multigeneblast_score",
color = category_colour,
range_x = [0, 100],
range_y = [0, max(df['multigeneblast_score']) * 1.1],
height = 750,
width = 2000)
if legend_display == 'dont_show_legend':
fig.update_layout(showlegend=False)
elif legend_display == 'show_legend':
fig.update_layout(showlegend=True)
else:
sys.exit()
fig.update_layout(transition_duration=500)
return [fig]
if __name__ == '__main__':
app.run_server(debug=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要解决此问题,请删除以下行:
似乎在更新布局和回调速度之间存在同步问题。回调迫不及待要更新布局以更新图例,它只会返回图形而无需更新。
To solve this problem remove the transition, the following line:
It seems that there is a problem of synchronization between updating the layout and the speed of callback. Callback cannot wait for the update of layout to update of the legend, and it only returns the figure without updating.