Redirect user to external URL

Hello, I’ve been learning Dash for the past two days and have found it very useful. Right now I have a WordPress site running on the same server as the Dash application. What I want to do is when particular data is inputted into the Dash interface, I want the Dash application to redirect the user to the WordPress site.

If I were to do what I want in Flask, it would look something like this:

from flask import Flask, redirect

app = Flask(__name__)

@app.route('/')
def hello():
    return redirect("http://www.example.com", code=302)

if __name__ == '__main__':
    app.run(host='0.0.0.0')

This code would effectively redirect the user to “http://www.example.com.”

Here is the code that I have right now. I stripped all of the code from the last lines for example reasons, but it gives the idea of what I want to do.

import dash
from dash.dependencies import Input, Output, Event
import dash_core_components as dcc
import dash_html_components as html





app = dash.Dash(__name__)

app.css.append_css({
    "external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})

app.layout = html.Div([
    html.H4(children='Pandas Label Maker'),

    dcc.Input(
        placeholder='Enter SKU...',
        id='sku',
        value=''

    ),


    html.Br(),


    #html.Label('Print Quantity: '),
    dcc.Input(
        placeholder='Enter Quantity...',
        id='print_quantity',
        value=''
    ),


    html.Br(),



    dcc.Input(
        placeholder='Enter Position...',
        id='position',
        value=''
    ),


    html.Br(),


    html.Label('Overlay: Yes or No?'),
    dcc.Dropdown(
        options=[
            {'label': 'Yes', 'value': 'Yes'},
            {'label': 'No',  'value':  'No'}
        ],
        id='overlay',
        value='No'
    ),


    html.Label('Ready to print?'),
    dcc.Dropdown(
        options=[
            {'label': 'Yes', 'value': 'Yes'},
            {'label': 'No',  'value':  'No'}
        ],
        id='print',
        value='No'
    ),

    html.Div(id='skuValues'),
    html.Br(),


    dcc.Markdown('''


Go Back to [Pandas](http://172.29.0.29/).


'''),
html.Br(),
html.Button('Default Values', id='default_values'),

], style={'color': 'Black', 'fontSize': 20, 'fontWeight': 'bold', 'fontFamily': 'Arial'})



@app.callback(
    dash.dependencies.Output('overlay', 'value'),
    events=[Event('default_values', 'click')]
)
def update():
    return 'No'

@app.callback(
    dash.dependencies.Output('print', 'value'),
    events=[Event('default_values', 'click')]
)
def update():
    return 'No'

@app.callback(
    dash.dependencies.Output('sku', 'value'),
    events=[Event('default_values', 'click')]
)
def update():
    return ''

@app.callback(
    dash.dependencies.Output('position', 'value'),
    events=[Event('default_values', 'click')]
)
def update():
    return ''

@app.callback(
    dash.dependencies.Output('print_quantity', 'value'),
    events=[Event('default_values', 'click')]
)
def update():
    return redirect("http://172.29.0.29/")




@app.callback(
    dash.dependencies.Output('skuValues', 'children'),
    [dash.dependencies.Input('sku', 'value'),
    dash.dependencies.Input('print_quantity', 'value'),
    dash.dependencies.Input('overlay', 'value'),
    dash.dependencies.Input('print', 'value'),
    dash.dependencies.Input('position', 'value')
    ])
def skuValue(sku, quantity, overlay, print_value, position):
    # Here's an example of what I want it to do.
    if sku == 'redirect':
        return redirect('http://mywordpresssite.com/')

Right now if I were to run this, everything would work except the redirect. So, my question is: how do I redirect to an external URL using Dash?

If I didn’t explain something well enough, please tell me and I will see if I can explain it better.

Thanks,
Malachi

1 Like

Good question. I think that would actually be possible using the dcc.Location component. See http://plot.ly/dash/urls for an example. You could return a new pathname in a callback that would update a single dcc.Location element.

Another solution would be to just dynamically update the href of a html.A link element and ask the user to click on that element in order to “redirect” themselves. That might be a more intuitive than redirecting on the behalf of the user anyway.

Can you explain how to return a new location from a callback? I have created a login page and on successful login, I want to redirect to another page say /home. Can you please show this with an example.

I have this as a question in : https://stackoverflow.com/q/48576712/5165377

Hey everyone! I had come across this post because I need to be able to redirect my user to an external link, which will eventually redirect them back to my website (i.e. I haven’t been able to get the dcc.Location to work because the base of the url needs to change.). Was wondering whether there’s a good way to do this all in Dash, or whether it makes more sense to just handle the entirety of the login process through Flask, and then once it’s all taken care of, hand the user off to the Dash application. (This is for login stuff). Thanks! :slight_smile:

I came here looking for the same thing. How to redirect an user from within a callback? My use case is the following: I have a dash_table component with a column which, when clicked, should redirect the user to another internal page. I can get the table, the click callback to work and the proper url to redirect using dash.page_registry, but I can’t find a way to redirect the user to the page I want.

In case someone has a similar use case as mine, I have just figured out that, for my case, I can use markdown to create links within a dash_table. That should address my issue. Found out about it here.

I am still looking forward to hear about the proper way to redirect a user from within a callback.

1 Like

Hi @fabiomolinar

You can find an example in the same repo that you found the markdown example - see example #15:

I didn’t realize that. Thank you for pointing that out Ann! It seems though that this approach triggers a page reload, right?

Hi @fabiomolinar

I just updated example #15 to show how to navigate to a new page without refreshing the page. I also added another example of how to navigate to a new page by clicking on a figure: GitHub - AnnMarieW/dash-multi-page-app-demos: Minimal examples of multi-page apps using the pages feature in dash>=2.5.1