✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

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...]

def index():
    return redirect(url_for('/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.