python 在DASH应用程序中更新子项时回调错误

ws51t4hk  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(132)

我得到的错误,而初始化的登录页面,这是第一页的应用程序以及.它运行正常,虽然在检查登录电子邮件.我无法理解的错误和它的决议.它看起来像登录页面上的布局的children属性不更新,因为它是需要.我得到的错误只是国家KeyError:‘孩子’

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

app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True
app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    
    html.Div(id='page-content',children = [
        html.H2('Login Page'),
        html.Div('Email'),
        dcc.Input(id='email', type='text', placeholder='Enter email'),
        html.Br(),
        html.Br(),
        html.Button('Login', id='login-button', n_clicks=0),
        html.Div(id='login-error')
    ], className='login-box')
])

success_page_layout = html.Div([
    html.H2('Success!'),
    html.P('You have successfully logged in.')
])

failure_page_layout = html.Div([
    html.H2('Failure!'),
    html.P('You have not been successfully logged in.')
])

@app.callback(Output('url', 'pathname'),
              [Input('login-button', 'n_clicks')],
              [State('email', 'value')])
def login(n_clicks, email):
    if n_clicks > 0:

        if email == 'example@example.com':
            return '/success'
        else:
            return '/failure'
    return '/'

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/success':
        return success_page_layout
    elif pathname == '/failure':
        return failure_page_layout
    else:
        return app.layout['children']

if __name__ == '__main__':
    app.run_server(debug=True)
vd8tlhqk

vd8tlhqk1#

我想我已经找到了问题所在,我需要在app.layout中使用id,而不是使用“children”,同时在回调中返回

return app.layout['page-content']

相关问题