【问题标题】:Authenticating a dash app within a flask app using firebase使用 firebase 在烧瓶应用程序中验证破折号应用程序
【发布时间】:2021-09-10 18:47:10
【问题描述】:

我正在使用 firebase 身份验证来验证我的烧瓶应用程序。这适用于所有烧瓶视图。但是,我无法让它与破折号视图一起使用。我在下面附上了一个非常简单的没有firebase的最小示例,其中我有一个烧瓶页面hello和一个dashboard页面。我用装饰器check_token() 包装视图,它目前应该只是将所有视图重定向到登录页面。这适用于烧瓶视图/hello/,但不适用于破折号视图dashboard

令人困惑的是,装饰器甚至没有为仪表板视图运行,打印语句证明了这一点。

routes.py

from flask import current_app as app
def check_token(f):
    print("RUNNING check_token")
    def wrap(*args,**kwargs):
        print("RUNNING wrap")
        token = request.cookies.get("token")
        if not token:
            return redirect(url_for('login'))
        return f(*args, **kwargs)
    wrap.__name__ = f.__name__
    return wrap

@app.route('/')
@app.route('/hello')
@check_token
def hello():
    return 'hello world!'

@app.route('/login')
def login():
    return "This is the login page"

@app.route('/dashboard')
@check_token
def render_dashboard():
    return redirect('/dash1')

application.py

from dash import Dash
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
import dash_html_components as html
from flask import Flask, request,render_template, url_for, redirect


def init_dash(dash_app1):
    dash_app1.layout = html.Div([html.H1('Hi there, I am app1 for dashboards')])
    return dash_app1

def init_app():
    server = Flask(__name__)
    dash_app1 = Dash(__name__, server = server, url_base_pathname='/dashboard/' )
    dash_app1 = init_dash(dash_app1)
    with server.app_context():
        import routes
    app = DispatcherMiddleware(server, {
        '/dash1': dash_app1.server,
    })
    return app

if __name__ == "__main__":
    run_simple('0.0.0.0', 8090, init_app(), use_reloader=True, use_debugger=True)

任何帮助将不胜感激!过去两天我一直在努力尝试完成这项工作

【问题讨论】:

    标签: authentication flask plotly-dash werkzeug


    【解决方案1】:

    我终于让它与我从一个不起眼的 github 线程中找到的代码 sn-p 一起工作

    from dash import Dash
    from werkzeug.middleware.dispatcher import DispatcherMiddleware
    from werkzeug.serving import run_simple
    import dash_html_components as html
    from flask import Flask, request,render_template, url_for, redirect
    from utils import check_token
    
    def protect_views(app,bpn):
        for view_func in app.server.view_functions:
            print(view_func)
            if view_func.startswith(bpn):
                app.server.view_functions[view_func] = check_token(app.server.view_functions[view_func])
        
        return app
    
    def init_dash(dash_app1):
        dash_app1.layout = html.Div([html.H1('Hi there, I am app1 for dashboards')])
        return dash_app1
    
    def init_app():
        server = Flask(__name__)
        bpn = '/dashboard/'
        dash_app1 = Dash(__name__, server = server, url_base_pathname=bpn )
        dash_app1 = init_dash(dash_app1)
        dash_app1 = protect_views(dash_app1, bpn)
        with server.app_context():
            # dash_app1 = protect_views(dash_app1)
    
            import routes
        app = DispatcherMiddleware(server, {
            '/dash1': dash_app1.server,
        })
        return app
    
    if __name__ == "__main__":
        run_simple('0.0.0.0', 8090, init_app(), use_reloader=True, use_debugger=True)
    
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 2021-07-15
      • 2021-03-09
      • 1970-01-01
      • 2015-09-07
      • 2021-09-11
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      相关资源
      最近更新 更多