Plotly Dash 应用程序中的重定向

Nik*_*aby 4 flask python-3.x plotly-dash

我一直在尝试从 Polty Dash 开发一个多页模板应用程序。我正在尝试在成功登录时实现重定向。该项目的结构如下:

multipage
??? app.py
??? apps
?   ??? app1.py
?   ??? app2.py
?   ??? app3.py
??? index.py
??? static
    ??? base.css
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

index.py(起点)

from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

from app import app, server
from apps import app1, app2, app3


app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/':
        return app3.layout
    else:
        return '404'

if __name__ == '__main__':
    server.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

在这里,当用户访问/登录页面(app3.py)时将呈现。

应用程序

import dash
import os

import flask


app = dash.Dash()
server = app.server
app.config.supress_callback_exceptions = True

external_css = [
    'https://codepen.io/chriddyp/pen/bWLwgP.css',
    '/static/base.css'
]
for css in external_css:
    app.css.append_css({"external_url": css})


@app.server.route('/static/<path:path>')
def static_file(path):
    static_folder = os.path.join(os.getcwd(), 'static')
    return flask.send_from_directory(static_folder, path)
Run Code Online (Sandbox Code Playgroud)

app2.py(登录代码)

import flask
from dash.dependencies import Input, Output, State, Event
import dash_html_components as html
import dash_core_components as dcc
from apps import app1

from app import app

layout = html.Div(children=[
    # content
    html.Div(id='login',children=[
        html.H3("Please log in", hidden=False, id="page_header"),
        # login form
        html.Form(children=[
            html.P(children=["Username: ", dcc.Input(type='text', id='username', placeholder='username')]),
            html.P(children=["Password: ", dcc.Input(type='password', id='password', placeholder='password')]),
            html.Button(children=['Login'], type='submit', id='login_button')
        ], style={'width': '30%', 'margin': '0 auto'}, id="login_form", hidden=False)
    ], style={'display': 'block', 'text-align': 'center', 'padding': 2}),
    html.Br(),
    html.Hr(style={'width': '30%'}),
])


@app.callback(Output('login', 'children'),
              events=[Event('login_button', 'click')],
              state=[State('username', 'value'), State('password', 'value')])
def login(username, password):
    if username:
        print("login")
        return flask.redirect('/home')
    else:
        print("No Luck")
Run Code Online (Sandbox Code Playgroud)

在函数中login(username, password),如果用户有效,那么应用程序应该重定向到/home并且app1.py必须在那里呈现。

我能够通过用户交互导航到各种页面。有没有一种方法可以让我从程序中重定向。我真的很新,请帮助我。

Kos*_*dis 8

这似乎不应该起作用,但它确实起作用:

首先为回调的输出添加一个隐藏的 div:

html.Div(id="hidden_div_for_redirect_callback")
Run Code Online (Sandbox Code Playgroud)

然后像这样定义你的回调(旁注:我正在使用flask_login):

# Note that I am currently at /login

@app.callback(Output("hidden_div_for_redirect_callback", "children"),
              [Input("login_button", "n_clicks")],
              [State('username_login', 'value'),
               State('pwsd_login', 'value'),])
def login_user_(n_clicks, uname, pswd):
    # User clicked the login button

    # Do login logic....

    if successful_login:
        return dcc.Location(pathname="/home", id="someid_doesnt_matter")

    else:
        # e.g. password doesn't match
        raise PreventUpdate()

Run Code Online (Sandbox Code Playgroud)

dcc.Location可悲的是,返回的内容将强制页面重新加载(带有任何相关的副作用),但您最终会进入您想要的路径。如果登录失败,您只需阻止更新。


Ole*_*nko 1

我提出了一个拉取请求,可以返回 Flask Response,但没有时间完成它。如果您需要在我通过测试覆盖此 PR 之前实现它,您可以通过此更改扩展基本应用程序并按如下方式使用它:

@app.callback(...)
def login():
    if username:    
        redirect_home = redirect('/home')
        response = app.make_response(redirect_home)
        return response
    else:
        return 'No luck'
Run Code Online (Sandbox Code Playgroud)

另一种黑客方法是在更改属性的标签内返回 JavaScript 片段window.location