Flask Form data used in Dash App

Hello.

I am working on a project that tries to integrate Flask and Dash together. Everything is coming along smoothly but I do not know how to use data from either Flask Form or Flask Login in my Dash app.

The idea is that a user logs into the app via Flask Login, can click on the navbar to go to the dashboard and then the dashboard is rendered for the data he owns (there will be multiple users and each user will have their own data stored in a database). Let’s say each row in the database has a column “owner” which value is an email of the owner that he uses to log in.

Everything else is working. I have my Flash app with multiple routes, a working Dash app under Flask, but I’m clueless as to how to pass logged user’s email from the login form to the SQL query used to get data for the dash dashboard.

I should say please assume I’m missing some obvious solution as this is my first personal project using both libraries.

Hello @zurfil,

Welcome to the community!

Assuming that you have your user logged in and everything is correct. You should be able to pass the current_user.id from flask-login wherever you would like.

With that being said, you need to make sure the any layout that you are using takes into account that there is an actual flask.request associated with it.

def layout():
    if not flask.request:
         return html.Div()
    if current_user.is_authenticated:
         # rest of code
    return html.Div()
2 Likes

Yes ofc, after actually doing it I feel dumb asking in the first place haha.

Your second point is very insightful, this did not even cross my mind. Thank you for that.

1 Like

No worries, we all learn together. :smiley: