簡體   English   中英

如何在 Dash App 中保持繪圖狀態

[英]How to persist state of plotly graph in Dash App

我有一個multi-tabsubtabs破折號應用程序。 我希望能夠在它們之間切換時在不同的子選項卡中保存/保持組件的狀態。 這些組件是dropdown, input, graph, slider, daterange等。

我使用組件的持久性屬性,它適用於輸入、下拉、滑塊但不適用於圖形。 我想保留dcc.Graph組件的狀態, dcc.Graph組件呈現繪圖可視化。

dcc.Tabs(

            id="tabs",
            vertical=True,
            persistence=True,

            children=[

                 dcc.Tab(label="Tab 1", value="tab1"),
                 dcc.Tab(label="Tab 2", value="tab2",
                         children=[dcc.Tabs(id="subtabs", 
                                            persistence=True, 
                                          
                            children=[dcc.Tab(label='a', value='ab'),
                                      dcc.Tab(label='z' value='wv')
                                     
                            ],

                    )
                 ]),

            ],
            
        )

dash 中是否有本地解決方案可以保存應用程序的狀態? 謝謝。

我在開發dash應用程序時遇到了這個問題。 在不同頁面之間切換時,我需要緩存數字。 嘗試存儲它們時調整 storage_type 對我有用: https : //dash.plotly.com/dash-core-components/store

偽代碼:

@app.callback(
    Output('plt','figure'),
    Input('url','pathname'), # use url to trigger callbacks
    Input('plt,'figure'),
    State('plt-cached','data'),
):
def render_fig(url,plt,plt-cached):
    if url != 'cached_page': raise PreventUpdate
    if not plt: # when switching between pages, dcc.Store('plt') is refreshed when set to 'memory'
        return plt-cached
    # other functions/code to render the plot in your own application

# callback functions to cache figure
@app.callback(
    Output('plt-cached','data'), # dcc.Store('plt-cached') is set to `storage_type=session`
    Input('plt','figure'),
)
def cache_fig(plt):
    return plt

我的實際應用程序比上面的例子復雜得多,如果你直接復制/粘貼上面的應用程序可能不起作用,因為我什至沒有干運行它。 但是,您可以獲得解決自己問題的線索:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM