Python 如何在dash核心组件中存储变量
我试图使用dash_core_components.store将变量存储到内存中,但它似乎没有将递增的数字保存到内存中。我想发生的是,每次我按下按钮,存储在内存中的数字都会增加10-相反,变量似乎没有保存,只输出15Python 如何在dash核心组件中存储变量,python,plotly-dash,Python,Plotly Dash,我试图使用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([
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。您需要做的是设置两个回调,如中所示。一个更新存储中的数据,另一个检索更新的数据。您没有输出到dcc.store
组件,因此它永远不会更改。这就是为什么它总是返回15。您需要做的是设置两个回调,如中所示。一个更新存储中的数据,另一个检索更新的数据