Mas*_*fox 4 python web-applications flask 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)
Run Code Online (Sandbox Code Playgroud)
小智 6
此行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])
Run Code Online (Sandbox Code Playgroud)
现在,“ app_dash”端点将受到保护。
如果您使用 阻止所有请求@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```
Run Code Online (Sandbox Code Playgroud)
现在将检查所有端点,甚至是 dash 应用程序端点。
添加这个名为 的装饰器public_route:
def public_route(function):
function.is_public = True
return function
Run Code Online (Sandbox Code Playgroud)
并将装饰器添加到公共方法中,例如登录、错误页面等。
@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
Run Code Online (Sandbox Code Playgroud)
这样您就不再需要 了@login_required,因为除非另有说明,否则所有端点都需要登录@public_route。
| 归档时间: |
|
| 查看次数: |
923 次 |
| 最近记录: |