0

我正在用 Python dash-plotly 编写一个多页应用程序。我的app.py -program具有以下布局结构:

app = dash.Dash(__name__)  

url_bar_and_content_div = html.Div([
    dcc.Location(id='url', refresh=True),
    html.Div(id='page-content'),
    dcc.Store(id='store1', storage_type='memory'),
    dcc.Store(id='store2', storage_type='memory')
])

layout_page1 = html.Div([ ... ])
 
layout_page2 = html.Div([ 
    ... 
    
    html.Div([
                    html.Button(id='submit-button',
                                        n_clicks=0,
                                        children='Submit',
                                        style={'background-color': '#84cc32', 'color': 'white'}
                    )], className="assay__element"),
    ...
  
    dcc.Store(id='page2-storage-1', storage_type='memory'),
    dcc.Store(id='page2-storage-2', storage_type='memory'),


])     
# so 2 pages in total

# index layout
app.layout = url_bar_and_content_div

# "complete" layout
app.validation_layout = html.Div([
    url_bar_and_content_div,
    layout_page1,
    layout_page2
])

# amongst callback-functions and hidden Divs in layout_pages etc.

结构基本遵循文档页面https://dash.plotly.com/urls 1-to-1 的第三个代码示例!

所以目前我正在尝试将layout_page2的两个存储数据('page2-storage-1'和'page2-storage-2')传输url_bar_and_content_div('store1'和'store2'),以及这是我失败的地方:

@app.callback([Output('store1', 'data'),       # simplified! (unnecessary lines discarded)
               Output('store2', 'data'),
               ...,
               Output('submit-button', 'n_clicks')],    # the "Submit"-button of page 2
               Input('submit-button', 'n_clicks'),      # it gets triggered by a click
              [State('page2-storage-1', 'data'),
               State('page2-storage-2', 'data')])
def submit_to_page1(n, stored_1, stored_2):
    if n>0:
        if stored_data is not None:
            #print(stored_1)
            #print(stored_2)
            return stored_1, stored_2, ...
        return dash.no_update, dash.no_update, ...
    return dash.no_update, dash.no_update, ...

@app.callback(Output(...),
             [Input('store1', 'data'),
              Input('store2', 'data')],
              State(...),
              prevent_initial_call=False)
def to_page2(stored_1, stored_2, ...):
    #print(stored_1)
    #print(stored_2)
    print("Hey")
    do stuff
    return ...

FIRST回调起作用,它是通过单击第 2 页上的 html-Button() 触发的,并且作为响应发送/返回两个保存的数据('page2-storage-1' 和 'page2-storage-2')到索引布局的保存位置(“store1”和“store2”)。我当然已经通过添加这两个命令行print(stored1&2)来测试回调是否有效,如代码中所示,以查看是否真的收到了数据(它是!)。

第二回调不起作用,我不知道为什么。在to_page1的参数中,我首先写了prevent_initial_call=False,以便在初始调用中执行回调,并且我还打印了索引布局的存储位置(第一个回调中的数据返回到的地方,那些输出) 而且一开始他们确实都是空的!所以这一定意味着这个回调应该被触发,因为它的两个输入之前是空的,但后来收到了前一个回调的数据!那么为什么它不被触发呢?当我禁用初始呼叫预防时,回调函数永远不会被触发,它永远不会执行,我知道因为打印的文本“嘿”永远不会出现在控制台上。那么为什么会这样呢?url_bar...div 的 dcc.Stores 是否在某个时间点被填满,或者返回的数据真的没有到达两个保存槽?还是我同时发送两个输入数据或类似的问题,甚至是一些语法问题?

我怎么知道??我怎样才能找出后一个回调没有被触发的原因是什么?是否有一些属性或命令等我可以使用它可以在某种程度上帮助我?还有另一个回调也没有被触发,但它或多或少与这里的情况相同,所以我可以修复它们。

如果有人在这里回复我的问题,我将非常感谢,我将不胜感激!提前致谢!

4

0 回答 0