Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在dash核心组件中存储变量_Python_Plotly Dash - Fatal编程技术网

Python 如何在dash核心组件中存储变量

Python 如何在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([

我试图使用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。您需要做的是设置两个回调,如中所示。一个更新存储中的数据,另一个检索更新的数据。

您没有输出到
dcc.store
组件,因此它永远不会更改。这就是为什么它总是返回15。您需要做的是设置两个回调,如中所示。一个更新存储中的数据,另一个检索更新的数据