I am using flask’ login module with my dash app and need to integrate dash app in Flask environment. I am using flask for authentication and have added a navigation menu that links to the dash app.
Here’s my file structure:
-
auth
- login
- server
- init.py
-
templates
- base.html
-
pages
- home.py
- page2.py
index.py
app.py
contents of app.py
import dash
import flask
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets,
)
server = app.server
contents of index.py
app.layout = html.Div([
# header
html.Div([
dcc.Link(
href=get_relative_path('/home'),
),
dcc.Link(
href=get_relative_path('/archive'),
),
dcc.Location(id='url'),
html.Div(id='page-content', style={'margin-left': '2%'}),
])
# Render page content
@app.callback(Output("page-content", "children"),
[
Input('url', 'pathname')
]
)
def display_content(pathname):
if pathname == '/home':
return home.layout
elif pathname == '/archive':
return archive.layout()
else:
return home.layout
contents of init.py
from flask_login import LoginManager, login_required
from .server import server
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
from index import app as dashApp
@server.route('/dashboard')
@login_required
def render_dashboard():
return redirect('/dashboard/')
app = DispatcherMiddleware(server, {
'/dashboard': dashApp.server,
#add dash routes
})
if __name__ == '__main__':
run_simple('0.0.0.0', 5000, app, use_reloader=True, use_debugger=True)
contents of server.py
from flask import Flask
server = Flask(__name__, instance_relative_config=False)
When I attempt to navigate / click the link to dash app, I get a 404 Not Found error. For some reason, upon debugging it seems that the def page_content
function is not running and app / page layout isn’t rendered.