Hi
I created a simple code that changes the value of a class static variable when a button is pressed using python dash.
And I want to set the running property by processing background=True in this callback function.
However, if background=True is set here, I have confirmed that value of Global.test in the code below does not change.
The update_clicks function first sets the value of Global.test to 5.
And btn_test outputs the changed Global.test value.
Since Global.test has been changed to 5, I expected 5 to be output in btn_test, but None is output. And if I delete background=True, Global.test in btn_test outputs 5 normally. Is there any way to change the value of Global.test while keeping the background=True property?
below is my all code.
import os
import dash
from dash import DiskcacheManager, CeleryManager, Input, Output, html
from Global import *
class Global():
test = None
if 'REDIS_URL' in os.environ:
# Use Redis & Celery if REDIS_URL set as an env variable
from celery import Celery
celery_app = Celery(__name__, broker=os.environ['REDIS_URL'], backend=os.environ['REDIS_URL'])
background_callback_manager = CeleryManager(celery_app)
else:
# Diskcache for non-production apps when developing locally
import diskcache
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)
app = dash.Dash(__name__, background_callback_manager=background_callback_manager)
app.layout = html.Div(
[
html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
html.Button(id="button_id", children="Run Job!"),
html.Button(id="button_test", children="Test Job!"),
html.Div('test', id='div_test')
]
)
@app.callback(
output=Output("paragraph_id", "children"),
inputs=Input("button_id", "n_clicks"),
background=True,
running=[
(Output("button_id", "disabled"), True, False),
],
prevent_initial_call=True
)
def update_clicks(n_clicks):
Global.test = 5
print('Run : ' + str(Global.test))
return [f"Clicked {n_clicks} times"]
@app.callback(
Output('div_test', 'children'),
Input('button_test', 'n_clicks'),
prevent_initial_call=True
)
def btn_test(n):
print('test : ' + str(Global.test))
return Global.test
if __name__ == "__main__":
app.run_server(debug=True)
`type or paste code here`