Dynamic layout based on user session

Hi, I want to create a dynamic layout based on the user session, the problem I’m facing is that I can only request Flask session from a request, I tried creating @server.route('/') but it’s not the page loaded when opening the dashboard. I’ve read here that I also need to assign app.layout to a function for the dynamic dashboard creation but I can’t put all of this together.
I’d like to have something like this when loading the dashboard:

def load_layout():
if session.get(‘user_id’):
return some_layout
else:
# create session
return some_other_layout

Hello, I know I’m very late to the party however I’m submitting this because I couldn’t find it online and this was in one of the first results on google,
What you will need is dash_auth for html basic authentication and to import flask.request

default_layout=html.Div… etc
Special_User_layout= html.Div etc…

def get_layout():
    try:
        username = request.authorization["username"]
        if username=='user_id':
            return Special_User_layout
        else:
            return default_layout
    except Exception:
        return default_layout
    
app.layout = get_layout

This will work, a try and except is needed because when you launch the app for the first time it will call the layout but it won’t have flask application context therefore that will generate an error.
You can have as many layouts as you want per user and the usernames are the ones you setup with dash_auth.

2 Likes