This is a simplified example of my script, but basically the issue that I am running into is as follows:
When I click the button on page 1, text is stored in my dcc.Store component, which can then be viewed on pages 2 and 3; however, when I return to page 1, the stored text is cleared and can no longer be accessed on pages 2 and 3 without reclicking the button. Is there any way around this (so that the stored value doesn’t get cleared when returning to page 1)?
I added print statements inside of the callbacks so that you can see the text inside the terminal whenever switching pages or clicking button (which is how I know that the stored value is being cleared)
Thank you in advance.
(Code) app.py:
import dash
from dash import dcc
import dash_bootstrap_components as dbc
import webbrowser
def open_browser(port):
webbrowser.open('http://localhost:{}'.format(port))
app = dash.Dash(
__name__,
use_pages=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
)
navbar = dbc.NavbarSimple(
dbc.Nav(
[
dbc.NavLink(page["name"], href=page["path"])
for page in dash.page_registry.values()
if page.get("top_nav")
],
),
brand="Test Application",
color="#171b26",
dark=True,
className="mb-2",
)
storage = dcc.Store(id = 'session-store', storage_type = 'session')
app.layout = dbc.Container(
[navbar, storage, dash.page_container],
fluid=True,
)
if __name__ == "__main__":
port = 8050
# Timer(1, open_browser, args=[port]).start()
open_browser(port)
app.run_server(debug=True, use_reloader=False)
(Code) Page1.py:
import dash
from dash import Dash, dash_table, dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
dash.register_page(__name__, path="/", top_nav=True)
layout = dbc.Container([
html.Button('click button', id = 'button'),
html.Div(id = 'display-stored-value'),
])
@callback(
Output(component_id = 'session-store', component_property = 'data'),
Input(component_id = 'button', component_property = 'n_clicks'),
prevent_initial_call = True
)
def to_storage(n_clicks):
if n_clicks:
text = 'You clicked button'
return text
@callback(
Output(component_id = 'display-stored-value', component_property = 'children'),
Input(component_id = 'session-store', component_property = 'data'),
prevent_initial_call = True
)
def to_div(stored_text):
print(stored_text)
return stored_text
(Code) Page2.py:
import dash
from dash import Dash, dash_table, dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
dash.register_page(__name__, top_nav=True)
layout = dbc.Container([
html.Div(id = 'display-stored-value2'),
])
@callback(
Output(component_id = 'display-stored-value2', component_property = 'children'),
Input(component_id = 'session-store', component_property = 'data'),
)
def to_div2(stored_text):
print(stored_text)
return stored_text
(Code) Page3.py:
import dash
from dash import Dash, dash_table, dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
dash.register_page(__name__, top_nav=True)
layout = dbc.Container([
html.Div(id = 'display-stored-value3'),
])
@callback(
Output(component_id = 'display-stored-value3', component_property = 'children'),
Input(component_id = 'session-store', component_property = 'data'),
)
def to_div3(stored_text):
print(stored_text)
return stored_text