Description
As far as I can tell, this flavor of the "duplicate dcc.Location callback" issue (#883) hasn't been documented yet. When I run a multipage app, reloading the page fires the callback twice with the same href
/ pathname
/ search
payload. This is distinct from the other issue of having the callback return None
and then returning the proper value(s).
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import dash
import urllib
navbar = dbc.NavbarSimple(
brand='Frac Planning App',
brand_href='/',
sticky='top',
)
body = dbc.Container(
dbc.Row(
dbc.Col(id='page-content')
)
)
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
navbar,
body
])
@app.callback(Output('page-content', 'children'),
[Input('url', 'href')],
[State('url', 'pathname'), State('url', 'search')])
def display_page(href, pathname, search):
print('HREF:', href)
print('PATHAME:', pathname)
print('SEARCH:', search)
url = urllib.parse.urlparse(href)
# print('RAW pathname:', url)
print('TRIGGER(S):', [i['prop_id'] for i in dash.callback_context.triggered])
if url.path == '/':
return form.layout
elif url.path == '/result':
query = dict(urllib.parse.parse_qsl(url.query))
return result.plot(**query)
else:
raise dash.exceptions.PreventUpdate
Here is the output. It doesn't always print in the same order:
HREF: http://houmedge201:5004/
PATHAME: /
HREF: http://houmedge201:5004/
SEARCH:
PATHAME: /
TRIGGER(S): ['url.href']
SEARCH:
TRIGGER(S): ['url.href']
Running on Linux. Versions used:
dash 1.7.0
dash-core-components 1.6.0
dash-html-components 1.0.2
dash-renderer 1.2.2
I've tried the fix mentioned in #883 (comment) to no avail. Solving this is mission critical for my app because the duplicate callback crashes a subsequent database query.
Changing href
via a html.Link
makes the callback fire only once. However, users need to be able to share app URLs with query params, which right now is broken due to this bug.