我尝试使用dash_core_components.Store将变量存储到内存中,但它似乎没有将递增的数字保存到内存中。我想要发生的是,每次我按下按钮,存储在内存中的数字增加了10 -相反,变量似乎没有保存,只是输出了15。
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash(__name__)
x = 5
app.layout = html.Div([
dcc.Store(id='memory-data', data = {'the-data': x}),
html.Div([
html.Button('click me', id='add-button')
]),
html.Div(id='debug-out'),
])
@app.callback(dash.dependencies.Output('debug-out', 'children'),
[dash.dependencies.Input('add-button', 'n_clicks')],
[dash.dependencies.State('memory-data', 'data')])
def button_pressed(clicks, data):
data['the-data'] += 10
return data['the-data']发布于 2020-07-05 06:10:29
您没有输出到dcc.Store组件,因此它永远不会改变。这就是为什么它总是返回15。你需要做的就是设置两个回调函数,就像在this example from the docs中一样。一个更新存储中的数据,另一个检索更新后的数据。
https://stackoverflow.com/questions/62731812
复制相似问题