Hi everyone.
I cannot find a way to “internal redirect” the user without refreshing the page, after the pressing of a button and checking some backend rules (inside the callback).
Imagine having a page (/my_table/new) with some form elements and a button. You want to submit it via the button and after creating an object in your database you want to change the location of the browser back to “/my_table” without refreshing the page.
You can a make the above scenario by placing a dcc.Location OUTPUT object to send the new pathname. You will see the browser location changing, but here is the problem. If you place another dcc.Location INPUT object to catch this browser’s change, this will never fire.
If you try the same with a dcc.Link everything works fine, including the INPUT location rule. But in that case you cannot check the form elements via a callback…
I am sending a code example to reproduce the problem.
Could anyone help me with this? Is there a possible via to do it?
Thank you!
from dash import Dash, no_update
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
app = Dash(__name__)
app.layout = html.Div([
dcc.Location(id='url-input'),
dcc.Location(id='url-output', refresh=False),
html.Button("Click me", id="button-a"),
dcc.Link("Click me", href='/link-a'),
html.Div('', id='clicks'),
]
)
@app.callback(Output('clicks', 'children'), [Input('url-input', 'pathname')])
def update_url_by_component_a(pathname):
print("pathname=", pathname)
return f"{pathname}"
@app.callback(Output('url-output', 'pathname'), [Input('button-a', 'n_clicks')])
def update_url_by_component_a(value):
if value:
print("value=", value)
return f"/clicks-{value}"
return no_update
if __name__ == '__main__':
app.run_server(debug=True)