如何将Flask登录名与Dash应用程序合并?

时间:2018-09-12 02:11:52

标签: python flask web-applications plotly-dash

我必须设计一个提供Flask服务和Dash服务的Web应用程序。例如,我想在Flask中创建一个结合Dash应用程序的登录名。问题是我无法将烧瓶登录名与破折号绑定。我需要一个类似“ @require_login”的方法来过滤对Dash服务的访问。 代码如下:

app_flask = Flask(__name__)

app_flask.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////login.db'
app_flask.config['SECRET_KEY'] = 'thisissecret'

db = SQLAlchemy(app_flask)
login_manager = LoginManager()
login_manager.init_app(app_flask)

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(30), unique=True)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

@app_flask.route('/')
def index():
    user = User.query.filter_by(username='admin').first()
    login_user(user)
    return 'You are now logged in!'

@app_flask.route('/logout')
@login_required
def logout():
    logout_user()
    return 'You are now logged out!'

@app_flask.route('/home')
@login_required
def home():
    return 'The current FLASK user is ' + current_user.username

# TODO how to add login_required for dash? 
app_dash = Dash(server=app_flask, url_base_pathname='/dash/')
app_dash.layout = html.H1('MY DASH APP')


if __name__ == '__main__':
    app_dash.run_server(debug=True)

3 个答案:

答案 0 :(得分:3)

此行app_dash = Dash(server=app_flask, url_base_pathname='/dash/')view_functions所标识的app_flask中创建新的url_base_pathname

您可以在创建app_flask.view_functions之前和之后调试和检查app_dash的值。

现在我们知道view_functions是由app_dash创建的,我们可以手动将login_required应用于它们。

for view_func in app_flask.view_functions:
    if view_func.startswith(app_dash.url_base_pathname):
        app_flask.view_functions[view_func] = login_required(app_flask.view_functions[view_func])
现在,“ app_dash”端点将受到保护。

答案 1 :(得分:3)

最好使用 const db = firebase.firestore(); // Get a new write batch let batch = db.batch(); // Set the value of 'Doc1' const doc1Ref = db.collection("col").doc("1"); batch.set(doc1Ref, { foo: "bar" }); // Set the value of 'Doc2' const doc2Ref = db.collection("col").doc("1").collection("subcol").doc("2"); batch.set(doc2Ref, { bar: "foo" }); // Commit the batch batch.commit().then(function () { // ... }); 阻止所有请求,并且仅在登录后或端点标记为公共时才允许请求。

@app.before_request

现在将检查所有端点,甚至连短划线的应用程序端点。

添加名为def check_route_access(): if request.endpoint is None: return redirect("/login") func = app.view_functions[request.endpoint] if (getattr(func, "is_public", False)): return # Access granted # check if user is logged in (using session variable) user = session.get("user", None) if not user: redirect("/login") else: return # Access granted``` 的装饰器:

public_route

并将装饰器添加到公共方法中,例如登录,错误页面等。

def public_route(function):
    function.is_public = True
    return function

这样,您将不再需要@public_route @app.route("/login") def login(): # show/handle login page # call did_login(username) when somebody successfully logged in def did_login(username): session["user"] = username ,因为除非@login_required另有说明,否则所有端点都需要登录。

答案 2 :(得分:0)

一种解决方案:从烧瓶中进行会话(使用cookie)

from flask import session

这是一个例子:

@login_manager.user_loader def load_user(user_id): # I think here it's good session["uid"] = user_id return User.query.get(int(user_id))

# TODO how to add login_required for dash? if "uid" in session : app_dash = Dash(server=app_flask, url_base_pathname='/dash/') app_dash.layout = html.H1('MY DASH APP')