Flask and Dash in the same application

Hello everyone, I have a Flask application and a Dash application, but I can’t encapsulate Dash within Flask so that I can use my @login_required decorator. Can someone help me? I’ll leave some example code below

init.py

from flask import Flask, request
from app.dash_app import create_dash_app

def init_app():
    app = Flask(__name__)
    app.config.from_object('app.config.Config')

    with app.app_context():
        dash_app = create_dash_app(app)

    return app

dash_app.py

import dash
from dash import Dash, html, dcc, callback, Output, Input, dash_table
from flask import Flask
from flask_login import login_required

@login_required
def create_dash_app(server):
    dash_app = dash.Dash(__name__, server=server, url_base_pathname='/dash/')
    
    //some code

    dash_app.layout = html.Div([
        dcc.Store(id="store", data={}),
        //code
    ])
    
    @callback's
     def functions:
          //code

    return dash_app, server

I’ve tried to explain how I’m doing it with the examples above. I would greatly appreciate it if someone could tell me how to make this work, as so far I’m running two separate and not integrated applications.

Hello @JackWtr,

As I mentioned in my previous comment in your original post, you cannot use the decorator as dash creates the routes in the background.

You can however create different things to keep your site protected:

from flask_login import login_required, current_user
def layout():
    if current_user:
        if !current_user.is_authenticated:
            return 'No access'
    else:
        return 'No access'
    return html.Div([
        dcc.Store(id="store", data={}),
        //code
    ])

dash_app.layout = layout

Then you can also use before_request to make sure the request is from a logged in user:

from flask import request, redirect
@server.before_request
def test_identity():
    if request.url[:6] == '/_dash':
        if current_user:
            if !current_user.is_authenticated:
                return redirect(pathforprotected)
        else:
            return redirect(pathforprotected)
   return

The above will lock down your entire dash app, except for assets, until you are logged in.