How could I override index route ('/') behavior?

Good evening,

I am trying to use Dash and Flask for the first time. My purpose is to develop a single page webapp with some graphs, restricted for few users only using Google OAuth2 API.

I have implemented all the Google OAuth2 logic by following this and this tutorials but I can’t figure out how to properly override the route of ‘/’.

Here is a snippet of my code:

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

# Setup the Flask server
server = Flask(__name__)

app = dash.Dash(__name__, server=server, url_base_pathname='/', external_stylesheets=external_stylesheets)

app.layout = [some fancy layout here...]

@server.route('/')
def index():
    return redirect(url_for('/login'))

@server.route('/login')
def login():
    [all my Google OAuth login logic here...]

    return redirect(url_for('/'))

[...]

if __name__ == '__main__':
    app.run_server(debug=True, ssl_context='adhoc')

It does not even redirect to the login page, it just displays my app layout as usual. I don’t really know if my problem is either Dash related or Flask related, but I would give a try to get some help on this forum. Can anyone help me on this one ?

Best regards,

Good evening,

I managed to figure out what my problem was and how to solve it.

The problem was that I was creating a route on my Flask server which then was overriden/shaded by my Dash app which creates its own routes in function init_app() (to debug this, you can print out the view_functions and url_map variables from your Flask server to see all the routes and all the linked view functions).

Since my purpose was to add a Google OAuth layer above Dash routes, I just removed the concerned Dash routes and kept mines from Flask server, and then call the index() function when a Google user is logged in. So I removed routes from url_map, _rules and _rules_by_endpoint, which is not clean but at least it works for my use-case.

Hello, I am having the same issue but can’t seem to figure out how to delete the routes. Would you mind sharing your code on how to remove routes from url_map , _rules and _rules_by_endpoint? Thank you

Hello @jdonnell416,

Welcome to the community!

I normally add my routes to the server before passing it to the dash app. In my experience, the flask server routes take priority as long as you add the routes before the app has been added. Especially for these default routes.

You can always add non-default routes for the index and stuff.