簡體   English   中英

如何修復 Dash 應用程序異常 - 預期的輸出值數量無效

[英]How to fix Dash App Exception - Invalid number of output values Expected

我使用plotly dash app框架來構建一個多頁應用程序。 我有多個輸出值的回調函數,一個回調用於 tab_content,另一個用於更新 page_content。

在部署應用程序時,當我單擊Home時它會引發異常並且不會在回調中呈現內容。 所需的行為是在單擊Home ,頁面內容刷新並呈現選項卡內容。


app.layout = html.Div([

    # header
    html.Div([ 

              dcc.Link(
                 href=app.get_relative_path('/home'),
                 children='Home'
              ),

              dcc.Link(
                 href=app.get_relative_path('/history'),
                 children='History'
              ),

              # For pdf downloads
              dash_extensions.Download(id="download"),
              dcc.Location(id="url"),

              # Page content
              html.Div(id="page-content"),

              # Tab content
              html.Div(id="tab_content"),

      ])

打回來

更新選項卡內容,如果 pagename == 'home' 然后返回 tab1 布局



@app.callback([
               Output('tab-content', 'children'),
               
              ],
              [ 
               Input('tab', 'value')
               Input('url', 'pagename'),
              ]
             )
def display_content(tab, pagename):

    # Pages and Links

    if tab == 'tab1' or pagename == 'home':
        return tab1.layout

    if tab == 'tab2':
        return tab2.layout()


# Update page content 

@app.callback([
               Output('page-content', 'children'),
               Output('download', 'data')
              ],
              [Input('url', 'pagename')]
             )
def display_content(page_name):

    # Pages and Links

    if not page_name or page_name == 'home':
        return [dash.no_update, dash.no_update]

    if page_name == 'history':
        return [history.layout(), dash.no_update]

    else:
        return ['404 Error', dash.no_update]

我也嘗試刪除列表並修改返回語句,但沒有運氣:

if not page_name:
   return dash.no_update, dash.no_update

if page_name == 'history':
   return history.layout(), dash.no_update

我在日志中看到以下回溯。


Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/.heroku/python/lib/python3.6/site-packages/dash/dash.py", line 1050, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File "/app/.heroku/python/lib/python3.6/site-packages/dash/dash.py", line 995, in add_context
_validate.validate_multi_return(output_spec, output_value, callback_id)
File "/app/.heroku/python/lib/python3.6/site-packages/dash/_validate.py", line 126, in validate_multi_return
callback_id, len(outputs_list), len(output_value)
172.17.0.19 - - [03/Sep/2020:06:03:32 +0000] "POST /dash-app/_dash-update-component HTTP/1.1" 500 69 "https://example.com/dash-app" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"

dash.exceptions.InvalidCallbackReturnValue: Invalid number of output values for ..page-content.children...download.data...

Expected the output type to be a list or tuple, but got None

您缺少關閉字符串的引號。 那應該解決它。


    if page_name == 'history': # this closing one here
        return [history.layout(), dash.no_update]

    else:
        return ['404 Error', dash.no_update]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM