使用dash.testing中的import_应用程序会在DuplicateCallbackOutput中产生测试结果

2024-04-26 03:42:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在两个文件中定义了一个plotly dash应用程序:

app.py

from flask import Flask
import dash
import dash_bootstrap_components as dbc

server = Flask(__name__)

app = dash.Dash(name='MYAPP', external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = 'MYAPP'
app.config.suppress_callback_exceptions = True

index.py

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

from app import app

srv = app.server

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

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname'),
               Input('url', 'hash'),])
def display_page(p_pathname, p_hash):
    # Some logic to serve different pages 
    # (...)

if __name__ == '__main__':
    app.run_server(processes=1, debug=True, threaded=True)

现在,当我尝试使用import_app运行多个测试时

tests\test\u something.py

from dash.testing.application_runners import import_app

def test_scenario_1(dash_duo):
     app = import_app('index')
     dash_duo.start_server(app)

     dash_duo.wait_for_element_by_id('page-content', timeout=30)

     # do some tests (...)

def test_scenario_2(dash_duo):
     app = import_app('index')
     dash_duo.start_server(app)

     # do other tests (...)

我在场景2中遇到一个错误:

dash.exceptions.DuplicateCallbackOutput: 
You have already assigned a callback to the output
with ID "page-content" and property "children". An output can only have
a single callback function. Try combining your inputs and
callback functions together into one function.

如何在没有错误的情况下运行这两个场景?这是Plotly Dash应用程序多个测试的正确方法吗


Tags: namefrompyimporttrueappserverhtml