你如何在破折号核心组件中存储变量
how do you store variables in dash core components
我正在尝试使用 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']
您没有输出到 dcc.Store
组件,所以它永远不会改变。这就是它一直返回 15 的原因。您需要做的是设置两个回调,例如 this example from the docs。一个更新存储中的数据,另一个检索更新后的数据。
我正在尝试使用 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']
您没有输出到 dcc.Store
组件,所以它永远不会改变。这就是它一直返回 15 的原因。您需要做的是设置两个回调,例如 this example from the docs。一个更新存储中的数据,另一个检索更新后的数据。