Reputation: 329
I am trying to store a variable into memory using dash_core_components.Store, but it doesn't seem to be saving the incremented number to memory. what I want to happen is everytime I press the button, the number stored in memory increases by 10 - instead the variable doesn't seem to be saving and just outputs 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']
Upvotes: 0
Views: 10202
Reputation: 6616
You aren't outputting to the dcc.Store
component, so it never changes. That's why it keeps returning 15. What you would have to do is set up two callbacks, like in this example from the docs. One updates the data in the store, and the other retrieves the updated data.
Upvotes: 2