使用 dcc.Store 在另一个页面中绘制 Dash 共享回调输入回调、页面、dcc、Store

2023-09-06 07:39:53 作者:余生无你

我有一个 2 页的应用程序,在第一页 (app.py) 上,我使用 dcc.Store 在会话缓存中存储一​​个值,然后尝试在第二页 (app2.py),并将其显示为 html.H1.

i have a 2-page app, on the first page (app.py), i use dcc.Store to store a value in the session cache, and then trying to load this data in the 2nd page (app2.py), and show it as html.H1.

这是我在第一页的代码:

Here is my code in page one:

dcc.Store(id='session', storage_type='session'), 

那么我在这个页面的回调是:

then my callback on this page is:

@app.callback(Output('session', 'data'),
              [Input('q1', 'value')])
def q1_value(q1):
     return {'answer1value': q1}

而q1";是我的 radioitem 中的一个值.

while "q1" is a value from my radioitem.

但是当我运行这个应用程序时,这个 H1 中没有显示任何内容.我花了很多时间来解决这个问题,但失败了,有人可以帮忙吗?

But when i run this app, nothing is shown up in this H1. I have spent many hours fixing this but fail, would anyone please help ?

推荐答案

把你的

dcc.Store(id='session', storage_type='session'),

到app.py,而不是page1.py,在

onto the app.py, not page1.py, under the

app.layout = html.Div([....])

那么你的值就会存储在这里,可以从其他页面调用.

then your value will be stored here, and can be called from other pages.