为什么我的DASH应用不使用放射性更新

发布于 2025-01-24 09:24:58 字数 2829 浏览 2 评论 0原文

在下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-01-31 09:24:58

要解决此问题,请删除以下行:

fig.update_layout(transition_duration=500)

似乎在更新布局和回调速度之间存在同步问题。回调迫不及待要更新布局以更新图例,它只会返回图形而无需更新。

To solve this problem remove the transition, the following line:

fig.update_layout(transition_duration=500)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文