How to combine Dash and Flask login without using iframe?

Issue

I have a flask application, some pages of the app is protected with Flask-Login, I need to add Dash plot to my application, the plot should be on the page which requires authentification. I should add that in addition to Flask Login, I created user access levels using flask session.

I found how to combine Dash with protected page using iframe block, but I would like to try to make dash plot as a separate page (not iframe) with user authentification. If forget about authentification I have no problen to add Dash plot as a separate page in Flask app.

Typically I do the following to check login:

@app.route("/somempage", methods=["GET", "POST"])
@flask_login.login_required
def return_somepage():
    active_username = session['user_id']
    try:
        HOST, USER, PASSWORD, AUTH_PLUGIN = 
        InitSetup.read_mysql_init_config_file(WORKDIR +
        os.path.join(os.getcwd(), "mosreg_webscrap_website.config.txt"))
        conn, curs = MySQLOP.create_mysql_connection(HOST, USER, PASSWORD, 
        AUTH_PLUGIN)
        active_user = MySQLOP.retrieve_userinfo_from_db(curs, 
        active_username)
#now we check value of db colum and decide if user can see the page:
if active_user[0][4]:
    #show page
else:
    # return user have no rights to see the page template

Solution

I successfully added @login_required in my dash app, using the following:

in __init__.py of flask app

app = Flask(__name__, instance_relative_config=False)

# other stuff ...

@app.route("/dash", methods=['POST','GET'])
@login_required
def dash():
    return appdash.index()

in dashapp.py

appdash = dash.Dash(__name__, external_stylesheets=external_stylesheets, server=app)

Notice that I did not define a route in the Dash app declaration (i.e. routes_pathname_prefix='/dash'), so Dash App may only be viewed through the route of my Flask app, to which I put @login_required.

@login_required in my case is the one from Flask tutorial

Answered By – Irene Vandorou

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published