Har*_*ron 5 python flask plotly-dash flask-jwt-extended
我有一个复杂的破折号应用程序,我想将其保留在受 JWT 保护的路线后面。我的最终目标是将它包含在单独路由上的 iframe 中,但我只希望用户能够获取 th dash 应用程序的 html,如果他们有访问令牌。
我已重试在获取请求中返回应用程序本身。
应用程序
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
Run Code Online (Sandbox Code Playgroud)
索引文件
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()
Run Code Online (Sandbox Code Playgroud)
我将分享我在研究这个问题时发现的一些资源:
dash-auth,您可以使用它来安装pip install dash-authimport dash_auth ”的子模块。根据中的文本,“PlotlyAuth”已被弃用from dash_auth.plotly_auth import deprecation_noticeResponse.set_cookie. 以下是有关该方法的详细信息,请注意作者使用的rep.set_cookie('custom-auth-session', username)| 归档时间: |
|
| 查看次数: |
1168 次 |
| 最近记录: |