Hello @AnnMarieW, thank you for your reply !
When I tried using a unique dcc.Location across the app, the same behavior persisted. the url is only called in the callback of the page I left.
Here’s a minimal example of the code in which I commented out the specific dcc.Locations and used a unique URL here:
app.py
from dash import Dash, html, dcc, dash
import dash_bootstrap_components as dbc
app = Dash(
__name__,
use_pages=True,
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP]
)
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
dash.page_container,
])
if __name__ == "__main__":
app.run_server(debug=True)
page1.py
from dash import Dash ,html, dcc, Input, Output, callback_context, register_page, callback
from dash import dash
import dash_bootstrap_components as dbc
from datetime import datetime
register_page(__name__, path="/", name="Page 1")
layout = html.Div([
html.H1("Page 1"),
dcc.Location(id="url-page1"),
dbc.NavbarSimple(
[
dbc.Button("page1", href="/", color="secondary", className="me-1"),
dbc.Button("page2", href="/page2", color="secondary"),
],
color="primary",
dark=True,
className="mb-2",
),
html.Div(id="data-output-page1"),
html.Button("Refresh Data", id="refresh-button-page1"),
])
@callback(
Output("data-output-page1", "children"),
# Input("url-page1", "pathname"),
Input("url", "pathname"),
Input("refresh-button-page1", "n_clicks"),
prevent_initial_call=True,
)
def refresh_data(pathname, n_clicks):
ctx = callback_context
triggered_id = ctx.triggered_id
print("callback of page 1, triggered_id : ",triggered_id)
if triggered_id == "url":
print("entered the if with url 1 triggered")
return f"Page 1 data loaded at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
elif triggered_id == "refresh-button-page1":
return f"Data manually refreshed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
return "No update"
page2.py
from dash import html, dcc, Input, Output, callback_context, register_page, callback
from dash import dash
import dash_bootstrap_components as dbc
from datetime import datetime
register_page(__name__, path="/page2", name="Page 2")
layout = html.Div([
html.H1("Page 2"),
dcc.Location(id="url-page2"),
dbc.NavbarSimple(
[
dbc.Button("page1", href="/", color="secondary", className="me-1"),
dbc.Button("page2", href="/page2", color="secondary"),
],
color="primary",
dark=True,
className="mb-2",
),
html.Div(id="data-output-page2"),
html.Button("Refresh Data", id="refresh-button-page2"),
])
@callback(
Output("data-output-page2", "children"),
# Input("url-page2", "pathname"),
Input("url", "pathname"),
Input("refresh-button-page2", "n_clicks"),
prevent_initial_call=True,
)
def refresh_data(pathname, n_clicks):
ctx = callback_context
triggered_id = ctx.triggered_id
print("callback of page 2, triggered_id : ",triggered_id)
if triggered_id == "url":
print("entered the if with url 2")
return f"Page 2 data loaded at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
elif triggered_id == "refresh-button-page2":
return f"Data manually refreshed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
return "No update"
- Terminal Output when I click on page2:
callback of page 1, triggered_id : url
entered the if with url 1 triggered
The callbacks are not triggered on page landing because of prevent_initial_call=True
(I need it in my actual code because I have duplicate callback outputs across the page)
- when I comment out the
prevent_initial_call=True
I get this as output in the Terminal when I click on page 1:
callback of page 2, triggered_id : URL
entered the if with url 2
callback of page 1, triggered_id : None
“None” shows that the id of dcc.Location is not triggered and I’m unsure why.
Are there other approaches to updating data when the page loads, or a way to keep the app continuously retrieving new data?
I hope this code illustrates the issue.
Thank you for your time!