首页 > 解决方案 > Dash Loading children - 页面刷新后内容的持久性

问题描述

我有以下 Dash 应用程序,其中回调函数有一些由按钮触发的逻辑,它更新 html.Div 组件的内容(id="run-button-feedback")并刷新页面。但是,我无法避免 html.Div 组件在页面刷新后返回其初始值。

页面刷新对于其他组件的更新是必不可少的,但是这个 html.Div 组件需要包含一条给用户的消息,该消息在页面刷新后仍然存在

我怎样才能做到这一点?

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.GRID])
app.config.suppress_callback_exceptions = True
server = app.server

app.layout = dbc.Container([
        dcc.Location(id="url", refresh=False, href='home/page/'),
        dbc.Row([
            dbc.Col([
                html.Span(
                    "RUN",
                    id="run-button",
                    n_clicks=0,
                    className="button",
                ),
            ]),
            dbc.Col([
                dcc.Loading(
                    id="run-button-loading",
                    children=[
                        html.Div(id="run-button-feedback")
                    ],
                    type="circle",
                ),
            ])
        ]),
    ], style={"margin": "0%"}, fluid=True)  # fluid=True for Container to fill horizontal space and resize fluidly


# callback logic
@app.callback(
                [Output('url', 'pathname'),
                 Output('run-button-feedback', 'children')],
                [Input('run-button', 'n_clicks')],
                [State('url', 'pathname')],
             )
def refresh_analysis(n_clicks, url):
    """
    Refresh page on which it was called
    """
    if n_clicks > 0:
        return url, "message that need to persist after page refresh <p>clicked {} times</p>".format(n_clicks)
    return url, "Initial value"


if __name__ == '__main__':
    app.run_server(debug=True)

标签: pythonplotly-dash

解决方案


推荐阅读