Here’s an example of “internal redirect” feature.
I think the key is to use the same dcc.Location instance
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, ALL
import json
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
# represents the URL bar, doesn't render anything
dcc.Location(id='url', refresh=False),
dcc.Link('Navigate to "/"', href='/'),
html.Br(),
dcc.Link('Navigate to "/page-1"', href='/page-1'),
html.Br(),
dcc.Link('Navigate to "/page-2"', href='/page-2'),
html.Br(),
dcc.Link('Navigate to "/form1"', href='/form1'),
html.Br(),
dcc.Link('Navigate to "/form2"', href='/form2'),
# content will be rendered in this element
html.Div(id='page-content')
])
def default(pathname):
return html.Div([
html.H3('You are on page {}'.format(pathname))
])
def page1():
return html.Div([
html.H3('You are on page 1')
])
def page2():
return html.Div([
html.H3('You are on page 2')
])
def create_form1():
title = html.Div([
html.H3("Form1")
])
textarea = html.Div([
html.P("Form1 text"),
dcc.Textarea(
id={'type': 'text-form', 'name': 'form1'},
style={'width': '100%', 'height': 200},
),
html.Button(
'Submit', id={'type': 'form-button', 'name': 'form1'}, n_clicks=0)
])
location = dcc.Input(id={'type': 'output-location', 'name': 'form'},
type="text", style={'display': 'none'})
return html.Div([title, textarea, location])
def create_form2():
title = html.Div([
html.H3("Form2")
])
textarea = html.Div([
html.P("Form2 text"),
dcc.Textarea(
id={'type': 'text-form', 'name': 'form2'},
style={'width': '100%', 'height': 200},
),
html.Button(
'Submit', id={'type': 'form-button', 'name': 'form2'}, n_clicks=0)
])
location = dcc.Input(id={'type': 'output-location', 'name': 'form'},
type="text", style={'display': 'none'})
return html.Div([title, textarea, location])
@app.callback(
Output({'type': 'output-location', 'name': 'form'}, 'value'),
[
Input({'type': 'form-button', 'name': ALL}, 'n_clicks')
],
[
State({'type': 'text-form', 'name': ALL}, 'value')
]
)
def create_form_callback(n_clicks, textarea_value):
ctx = dash.callback_context
if ctx.triggered:
# Do something with input data...
print(n_clicks, textarea_value)
# Find what form initiated the callback
clicked = json.loads(
ctx.triggered[0]['prop_id'].split('.')[0]).get('name')
# Return to URL (will be processed by display_page callback)
return "/location/to/return/after/submit/{}".format(clicked)
else:
return dash.no_update
@app.callback(
Output('url', 'pathname'),
[
Input({'type': 'output-location', 'name': ALL}, 'value'),
],
)
def update_location(values):
ctx = dash.callback_context
if ctx.triggered:
return values[0]
else:
return dash.no_update
@app.callback(Output('page-content', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/page-1':
return page1()
elif pathname == '/page-2':
return page2()
elif pathname == '/form1':
return create_form1()
elif pathname == '/form2':
return create_form2()
else:
return default(pathname)
if __name__ == '__main__':
app.run_server(debug=True)