亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 current_user 從 Flask-Login 傳遞到 Plotly Dash 應用程序

將 current_user 從 Flask-Login 傳遞到 Plotly Dash 應用程序

慕桂英546537 2022-06-22 17:12:00
我正在嘗試構建一個集成在 Flask 應用程序中的 Dash 應用程序。一切似乎都運行良好,但是當我嘗試在 Dash 應用程序中顯示登錄用戶時,它顯示為“無”。我的應用程序結構如下:example/example/  dashapp/    static/      custom-css.css    templates/      base.html      home.html      login.html    __init__.py    app1.py    forms.py    models.py    routes.py  application.py  config.py  users.db我的 Dash 應用程序位于app1.py. 我嘗試了幾種方法通過current_user但沒有成功。雖然它出來很好home.html。我想問題在于我的應用程序位于單獨的文件中,而不是routes.py.這是代碼app.py:import dashimport dash_html_components as htmlfrom dashapp import applicationfrom flask_login import login_required, current_userapp1 = dash.Dash(__name__, server = application, routes_pathname_prefix = '/app1/', assets_folder = 'static', assets_url_path = '/static')app1.scripts.config.serve_locally = Trueapp1.css.config.serve_locally = Trueapp1.layout = html.Div(    children = '{}'.format(current_user))for view_func in app1.server.view_functions:    if view_func.startswith('/app1/'):        app1.server.view_functions[view_func] = login_required(app1.server.view_functions[view_func])routes.py代碼:from flask import render_template, flash, redirect, url_for, requestfrom flask_login import login_user, logout_user, current_user, login_requiredfrom werkzeug.urls import url_parsefrom dashapp import application, dbfrom dashapp.forms import LoginFormfrom dashapp.models import Userfrom dashapp import [email protected]('/')@application.route('/home')@login_requireddef home():    return render_template('home.html')其他腳本幾乎是標準的,除非真的需要它們,否則我不會將它們包含在問題中。請建議如何克服我的問題。謝謝!
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

我設法在一些幫助下解決了這個問題。以防萬一有人卡住,請參閱下面的更新代碼。


添加session了行以將當前用戶名存儲在routes.py:


from flask import render_template, flash, redirect, url_for, request, session

from flask_login import login_user, logout_user, current_user, login_required

from werkzeug.urls import url_parse

from dashapp import application, db

from dashapp.forms import LoginForm

from dashapp.models import User

from dashapp import app1


@application.route('/')

@application.route('/home')

@login_required

def home():

    return render_template('home.html')


@application.route('/login', methods=['GET', 'POST'])

def login():

    if current_user.is_authenticated:

        session['username'] = current_user.username

        return redirect(url_for('home'))

    form = LoginForm()

    if form.validate_on_submit():

        session['username'] = form.username.data

        user = User.query.filter_by(username=form.username.data).first()

        if user is None or not user.check_password(form.password.data):

            flash('Invalid username or password')

            return redirect(url_for('login'))

        login_user(user, remember=form.remember_me.data)

        next_page = request.args.get('next')

        if not next_page or url_parse(next_page).netloc != '':

            next_page = url_for('home')

        return redirect(next_page)

    return render_template('login.html', form=form)


@application.route('/logout')

def logout():

    session.pop('username', None)

    logout_user()

    return redirect(url_for('login'))

在session回調中app1.py:


import dash

import dash_html_components as html

from dash.dependencies import Input, Output

from dashapp import application

from flask_login import login_required

from flask import session


app1 = dash.Dash(__name__, server = application, routes_pathname_prefix = '/app1/', assets_folder = 'static', assets_url_path = '/static')

app1.scripts.config.serve_locally = True

app1.css.config.serve_locally = True


app1.layout = html.Div(

    children = [

        html.Div(id='div2'),

        html.Div(id='div3', children = 'xxxx'),

    ],

)


@app1.callback(

    Output('div2', 'children'),

    [Input('div3', 'children')])

def update_intervalCurrentTime(children):

    return session.get('username', None)


for view_func in app1.server.view_functions:

    if view_func.startswith('/app1/'):

        app1.server.view_functions[view_func] = login_required(app1.server.view_functions[view_func])



查看完整回答
反對 回復 2022-06-22
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

面臨同樣的問題。我想問題是當“應用程序”注冊 Dash 應用程序時當前用戶尚未初始化。很好的解決方法,雖然看起來有點不安全,因為 Flask 會話只是被編碼的。


https://github.com/RafaelMiquelino/dash-flask-login/blob/master/app.py - 我認為更強大的解決方案。它在回調中檢查 current_user 并將頁面的內容用作輸入,因此在頁面加載時調用回調。如果用戶體驗不是問題,您也可以跳過對視圖的保護。


這是我如何使用它的簡化示例:


@app.callback(

    Output('graph-wrapper', 'children'),

    [Input('page-content', 'children')])

def load_graph(input1):

    if current_user.is_authenticated:

        return dcc.Graph() # Dash Graph populated with current_user data from db

    else:

        return ''


查看完整回答
反對 回復 2022-06-22
  • 2 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號