我一直在尝试从Plolty Dash开发一个多页模板应用程序。我正在尝试通过成功登录实现重定向。该项目的结构如下:
multipage
├── app.py
├── apps
│ ├── app1.py
│ ├── app2.py
│ ├── app3.py
├── index.py
└── static
└── base.css
我的代码如下:
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)
此时,当用户访问/
时,将呈现登录页面(app3.py)。
app.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)
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")
在函数login(username, password)
中,如果用户有效,则应用应重定向到/home
,并且必须在那里呈现app1.py
。
我能够通过用户交互导航到各种页面。有没有办法在破折号中我可以从程序重定向。我真的很潇洒,请帮助我。
答案 0 :(得分:0)
我做了一个pull request,有可能返回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'
另一种黑客方法是在更改window.location
属性的标记内返回一个javascript片段。
答案 1 :(得分:0)
这似乎不应该起作用,但确实可以:
首先为回调的输出添加一个隐藏的div:
html.Div(id="hidden_div_for_redirect_callback")
然后像这样定义您的回调(旁注:我正在使用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()
返回的dcc.Location
会令人遗憾地迫使页面重新加载(具有任何相关的副作用),但是您将最终到达所需的路径。如果登录失败,则阻止更新。