How to combine Dash and Flask login without using iframe?
Question:
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
Answers:
I have manged to do that by using @login_required coupled with an in function condition to check if the user is logged in and authorized.
If not it will redirect him to the login page.
@main_bp.route('/', methods=['GET'])
@login_required
def dashboard():
return protected_view()
Adding more details..
## app.py
import flask_login
from flask_login import login_required
from flask_login import LoginManager
login_manager = LoginManager()
app = Flask(__name__, instance_relative_config=False)
login_manager.init_app(app)
with app.app_context():
# Import parts of our application
from visual import routes, auth
# Register Blueprints
app.register_blueprint(routes.main_bp)
app.register_blueprint(auth.auth_bp)
# Create Database Models
db.create_all()
## routes.py
from flask_login import current_user
from flask import current_app as app
from .assets import compile_auth_assets
from flask_login import login_required
# Blueprint Configuration
main_bp = Blueprint('main_bp', __name__,
template_folder='templates',
static_folder='static')
compile_auth_assets(app)
@main_bp.route('/', methods=['GET'])
@login_required
def dashboard():
return protected_view()
This tutorial should be useful..
https://scotch.io/tutorials/authentication-and-authorization-with-flask-login
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
Hi @Artiom Kozyrev and Others ! Could you please explain or provide the solution for how to embed the Authentication Enabled Dash Webpage in an HTML page using ?
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
I have manged to do that by using @login_required coupled with an in function condition to check if the user is logged in and authorized.
If not it will redirect him to the login page.
@main_bp.route('/', methods=['GET'])
@login_required
def dashboard():
return protected_view()
Adding more details..
## app.py
import flask_login
from flask_login import login_required
from flask_login import LoginManager
login_manager = LoginManager()
app = Flask(__name__, instance_relative_config=False)
login_manager.init_app(app)
with app.app_context():
# Import parts of our application
from visual import routes, auth
# Register Blueprints
app.register_blueprint(routes.main_bp)
app.register_blueprint(auth.auth_bp)
# Create Database Models
db.create_all()
## routes.py
from flask_login import current_user
from flask import current_app as app
from .assets import compile_auth_assets
from flask_login import login_required
# Blueprint Configuration
main_bp = Blueprint('main_bp', __name__,
template_folder='templates',
static_folder='static')
compile_auth_assets(app)
@main_bp.route('/', methods=['GET'])
@login_required
def dashboard():
return protected_view()
This tutorial should be useful..
https://scotch.io/tutorials/authentication-and-authorization-with-flask-login
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
Hi @Artiom Kozyrev and Others ! Could you please explain or provide the solution for how to embed the Authentication Enabled Dash Webpage in an HTML page using ?