Dash pages, redirect_from=['/'] Error

Hi, I am trying to redirect from ‘/’ to ‘/home’
My code:

dash.register_page(
    __name__,
    path='/home',
    redirect_from=['/'],
    title='home'
)

I get the next error:
ASSERTION ERROR:
view function mapping is overwriting an existing endpoint function: ‘/’

I don’t have any page or even a link that goes to ‘/’

Any ideas?

1 Like

Hello @Matan,

You could use flask redirect instead. You can do this by spinning up the flask server and adding the routing, then passing the server to dash as the server.

Something like this:

from flask import Flask, redirect
from dash import Dash, html, page_container
import dash

server = Flask(__name__)

@server.route('/')
def index_redirect():
    return redirect('/home')

app = Dash(__name__, server=server, use_pages=True, pages_folder='')

dash.register_page('Home', path='/home', layout='You are home')

app.layout = html.Div([page_container])

app.run()
2 Likes

@jinnyzor I prefer to stay with dash and not use flask.

Just for clarification, dash is built on top of flask, you would be just making basic adjustments to the server.

But…
My guess is that your app is defaulting to ‘/’ as the layout. Thus, technically you are trying to overwrite a registered page.

@jinnyzor Yes I know it’s built on top of flask, but if dash pages are a thing there are certain things that are supposed to be possible to do with them. now if it’s not possible, I will ask the dash team if it’s possible to add the feature in one of the next versions.

@Matan

Typically you would register your home page like this:

home.py

dash.register_page(
    __name__,
    path='/',    
    title='home'
)

Yea i know, it was a special case they asked me to make the home page at the ‘/home’.

What you could do, is have the page index redirect to dcc.Location via a callback.

Check out my redirect from logout to login on the flask login flow I posted recently.

I have it delay, but you could do it immediately.

I was only interested to know if there was a feature in dash pages that could do this, if not its not worth adding a callback for I convinced them it was not worth the trouble. but thanks.

Works perfectly!

1 Like

If there is a url_base_pathname, how should the root path be set?
I hope you will be able to provide the information.

Hello @tussiiiiipy,

Welcome to the community!

How exactly is this url_base_pathname added to your urls? Is it exposed on a server or something, are you passing it through a reverse proxy?

In the meantime, you can do this:

from flask import Flask, redirect, request
from dash import Dash, html, page_container
import dash

server = Flask(__name__)

app = Dash(__name__, server=server, use_pages=True, pages_folder='',
           url_base_pathname='/testing/')

@server.before_request
def index_redirect():
    if request.method == 'GET':
        if request.path == app.config['url_base_pathname']:
            return redirect(f"{app.config['url_base_pathname']}home")

dash.register_page('Home', path='/home', layout='You are home')

app.layout = html.Div([page_container])

app.run(debug=True)

Hello @jinnyzor, Thanks for your help.

It is published on waitress using the HttpPlatform of iis.
The URL is
http://[ipAdress]/url_base_pathname

The flask redirect worked fine when url_base_pathname was not set in dash.
However, when url_base_pathname was set, routing did not work.
I tried the following:
@server.route(‘/’)
@server.route(‘/[url_base_pathname]’)

By the way
I tried the implementation with dcc.Location and @callback and was able to redirect.

Yes, that would work.

That might be how you have to do it. If you were using a reverse proxy instead of a routing map, you could just have the server be the basic pathname.

Thanks for the information!
Unfortunately, it seems that it is not possible to change the server name as we are planning to carpool to an existing WebAPPserver.

I will try to use dcc.Location and @callback this time.

I know nginx can work with and route requests to different servers.

IIS may be able to rewrite the request to the backend server to make it more inline, but who knows.

1 Like