Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

820
Views
Passing current_user from Flask-Login to Plotly Dash app

I am trying to build a Dash app which is integrated in Flask app. Everything seems to be working fine but when I try to show logged user in the Dash app it comes out as 'None'.

My app structure is as below:

example/
example/
  dashapp/
    static/
      custom-css.css
    templates/
      base.html
      home.html
      login.html
    __init__.py
    app1.py
    forms.py
    models.py
    routes.py
  application.py
  config.py
  users.db

My Dash app is in app1.py. I've tried several ways to pass current_user but no success. It comes out fine in home.html though. I guess the problem is with my app being in separate file and not in the routes.py.

Here is the code for app.py:

import dash
import dash_html_components as html
from dashapp import application
from flask_login import login_required, current_user


app1 = dash.Dash(__name__, server = application, routes_pathname_prefix = '/app1/', assets_folder = 'static', assets_url_path = '/static')
app1.scripts.config.serve_locally = True
app1.css.config.serve_locally = True

app1.layout = html.Div(
    children = '{}'.format(current_user)
)

for view_func in app1.server.view_functions:
    if view_func.startswith('/app1/'):
        app1.server.view_functions[view_func] = login_required(app1.server.view_functions[view_func])

routes.py code:

from flask import render_template, flash, redirect, url_for, request
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.urls import url_parse
from dashapp import application, db
from dashapp.forms import LoginForm
from dashapp.models import User
from dashapp import app1


@application.route('/')
@application.route('/home')
@login_required
def home():
    return render_template('home.html')

@application.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('home')
        return redirect(next_page)
    return render_template('login.html', form=form)

@application.route('/app1/')

@application.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('login'))

Other scripts are pretty much standard and I won't include them in the question unless they are really needed.

Please suggest how to overcome my problem. Thanks!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I managed to fix this with some help. Just in case somebody gets stuck see below updated code.

Added session rows to store current username in routes.py:

from flask import render_template, flash, redirect, url_for, request, session
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.urls import url_parse
from dashapp import application, db
from dashapp.forms import LoginForm
from dashapp.models import User
from dashapp import app1

@application.route('/')
@application.route('/home')
@login_required
def home():
    return render_template('home.html')

@application.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        session['username'] = current_user.username
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        session['username'] = form.username.data
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('home')
        return redirect(next_page)
    return render_template('login.html', form=form)

@application.route('/logout')
def logout():
    session.pop('username', None)
    logout_user()
    return redirect(url_for('login'))

And session in the callback for app1.py:

import dash
import dash_html_components as html
from dash.dependencies import Input, Output
from dashapp import application
from flask_login import login_required
from flask import session

app1 = dash.Dash(__name__, server = application, routes_pathname_prefix = '/app1/', assets_folder = 'static', assets_url_path = '/static')
app1.scripts.config.serve_locally = True
app1.css.config.serve_locally = True

app1.layout = html.Div(
    children = [
        html.Div(id='div2'),
        html.Div(id='div3', children = 'xxxx'),
    ],
)

@app1.callback(
    Output('div2', 'children'),
    [Input('div3', 'children')])
def update_intervalCurrentTime(children):
    return session.get('username', None)

for view_func in app1.server.view_functions:
    if view_func.startswith('/app1/'):
        app1.server.view_functions[view_func] = login_required(app1.server.view_functions[view_func])
over 4 years ago · Santiago Trujillo Report

0

Faced the same problem. I guess the problem is that current user hasn't been initialised when the 'application' registers Dash app. Nice workaround, though seems a bit insecure since Flask sessions are just encoded.

https://github.com/RafaelMiquelino/dash-flask-login/blob/master/app.py - more robust solution in my opinion. It checks current_user in the callbacks and uses contents of the page as Input, so the callbacks are called on page load. You can also skip the protection of the views if user experience isn't an issue.

Here's a simplified example of how I used it:

@app.callback(
    Output('graph-wrapper', 'children'),
    [Input('page-content', 'children')])
def load_graph(input1):
    if current_user.is_authenticated:
        return dcc.Graph() # Dash Graph populated with current_user data from db
    else:
        return ''
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!