Flask-login current_user with dash

Hello everyone, this is my first post here at the forum. After looking up some weeks on it, searching and studying, i finally findout how to mix dash application with flask-login, so now my dashboard is only visiable for those who are logged in.

Buuut… Theres always a but.

What i need is to make a personalized dashboard for each user. The layout should be the same, the data is what i need to be different from each other, basically, i need to get the “User.name” (example) to make the DB request.

The problem is that i don’t know how to import “User.name” without creating a circular import.

Hello eduardorco,

Welcome to the community.

There are probably multiple ways to achieve it.

What I would do is the following:

  1. Set the values you need in your flask login route:
            user_dict = {
                key: user.__dict__[key]
                for key in ["id", "email", "is_admin"]
            }
            db.session.commit()
            session["user"] = user_dict
  1. Structure the layout of each page as function:
from flask import current_app, session
register_page(__name__, name="Series Explorer", path=current_app.config["URL_EXPLORER"])

def layout(**kwargs):
    user = session.get("user", False)

    if not user:
        return dmc.Container()

    return full_layout()


def full_layout():
    return dmc.Container(

You can access the session cookie before returning the layout.
Now you have two options:

  1. Either load the data on loading the page, you already have the user
  2. Or access the session cookie in a callback and use it for your db request dynamically.

I hope that helps :slight_smile:

2 Likes

Thanks! It helped alot!

Now i can progress on my project. Thx alot