本文介绍了如何在受保护的路线后面保留Plotly dash应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个绘图仪表板应用程序,我想将其保留在受JWT保护的路线后面.我的最终目标是在单独的路由中将其包含在iframe中,但我只希望用户如果具有访问令牌,就能够获得dash应用程序的html.
I have a plotly dash app which I would like to hold behind a route that is protected with a JWT. My end goal is to contain this in an iframe on a separate route, but I only want the user to be able to get the html of th dash app if they have an access token.
我尝试在get请求中返回应用程序本身.
I have retried returning the app itself in a get request.
App.py
import dash
from flask import Flask, jsonify, request
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token,
get_jwt_identity
)
server = Flask(__name__)
server.config['JWT_SECRET_KEY'] = 'super-secret' # Change this!
jwt = JWTManager(server)
@server.route('/login', methods=['POST'])
def login():
if not request.is_json:
return jsonify({"msg": "Missing JSON in request"}), 400
username = request.json.get('username', None)
password = request.json.get('password', None)
if not username:
return jsonify({"msg": "Missing username parameter"}), 400
if not password:
return jsonify({"msg": "Missing password parameter"}), 400
if username != 'test' or password != 'test':
return jsonify({"msg": "Bad username or password"}), 401
# Identity can be any data that is json serializable
access_token = create_access_token(identity=username)
return jsonify(access_token=access_token), 200
@server.route('/')
@jwt_required
def index():
return 'Hello world flask app'
app = dash.Dash(
__name__,
server=server,
routes_pathname_prefix='/'
)
app.config.suppress_callback_exceptions = True
Index.py
from app import app
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
from combination_1 import Combination
import callbacks
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id="root_div")
])
@app.callback(
Output('root_div', 'children'),
[Input('url', 'pathname')]
)
def attatch_graphs(pathname):
return Combination(comb_id='comb_1').return_div()
if __name__ == '__main__':
app.run_server()
推荐答案
我将分享一些研究该问题的资源:
I'll share a few resources I found researching the question:
- Dash提供了一个名为
dash-auth
的库,您可以使用pip install dash-auth
进行安装 - 一旦安装了库,就可以使用
import dash_auth
,并且您会注意到 HTTP基本身份验证, OAuth 和" PlotlyAuth "."PlotlyAuth";已根据dash_auth.plotly_auth import deprecation_notice 中 - 此模块的源代码似乎是 https://github.com/plotly/dash-验证
- 可在 https://dash.plotly.com/authentication 和促销页面上找到文档可从 https://plotly.com/dash/authentication/ 获得
- 大概可以使用Flask的
Response.set_cookie
集成JWT.这里是该方法的详细信息,请注意作者使用了rep.set_cookie('custom-auth-session',用户名)
- Github上有一个使用 Dash,Flask,和Flask-Login .此配置还存在于单独的StackOverflow问题中.但是,该库不支持JWT .您可以使用Flask-JWT-Extended库实现类似的体系结构.
中的文本被弃用
Dash provides a library named
dash-auth
, which you can install usingpip install dash-auth
Once you install the library, you'll be able to use
import dash_auth
, and you'll notice sub-modules for HTTP Basic Auth, OAuth, and "PlotlyAuth". "PlotlyAuth" has been deprecated, according to the text infrom dash_auth.plotly_auth import deprecation_notice
The source code for this module appears to be https://github.com/plotly/dash-auth
Documentation is available at https://dash.plotly.com/authentication and a promotional page is available at https://plotly.com/dash/authentication/
Presumably, JWT can be integrated using Flask's
Response.set_cookie
. Here are details about that approach, notice the author usesrep.set_cookie('custom-auth-session', username)
There's an example on Github which uses Dash, Flask, and Flask-Login. This configuration also exists in a separate StackOverflow question. However, this library doesn't supports JWT. You may be able to achieve a similar architecture using the Flask-JWT-Extended library.
这篇关于如何在受保护的路线后面保留Plotly dash应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!