I would like to update the attributes of objA
with long_callback update_output
, show the progress, and access the result with callback access_val
.
from dash import Dash, html, Input, Output, callback_context
import diskcache
from dash.long_callback import DiskcacheLongCallbackManager
cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(cache)
class ObjectB(object):
def __init__(self, val):
self.val = val
class ObjectA(object):
def __init__(self):
self.obj = ObjectB(10)
def update(self):
self.obj.val2 = 20
def get_val2(self):
return self.obj.val2
class DashObject(object):
def __init__(self, objA):
self.app = Dash(__name__, long_callback_manager=long_callback_manager)
self.objA = objA
def run_once(self):
self.app.layout = html.Div([
html.Button('Submit', id='submit-val', n_clicks=0),
html.Div(id='container-button-basic',
children='Enter a value and press submit'),
html.Button('Submit2', id='submit-val-2', n_clicks=0),
html.Div(id='container-button-basic-2',
children='Enter a value and press submit'),
html.Progress(id="progress-bar")
])
self.setup_callbacks()
self.app.run_server(debug=True)
def update(self):
self.objA.update()
def get_val(self):
return self.objA.get_val2()
def setup_callbacks(self):
@self.app.long_callback(
output = Output('container-button-basic', 'children'),
inputs = Input('submit-val', 'n_clicks'),
progress = [Output("progress-bar", "children")],
prevent_initial_call=True,
)
def update_output(set_progress, n_clicks):
changed_id = [p['prop_id'] for p in callback_context.triggered][0]
if 'submit-val' in changed_id:
self.update()
set_progress((str(1), str(1)))
return 'The input value was "{}"'.format(self.get_val())
@self.app.callback(
Output('container-button-basic-2', 'children'),
Input('submit-val-2', 'n_clicks'),
prevent_initial_call=True
)
def access_val(n_clicks):
changed_id = [p['prop_id'] for p in callback_context.triggered][0]
if 'submit-val-2' in changed_id:
return 'The input value was "{}"'.format(self.get_val())
if __name__ == "__main__":
objA = ObjectA()
Dash_obj = DashObject(objA)
Dash_obj.run_once()
In the dash app, I first clicked ‘Submit’ to set ObjB.val2 and then clicked ‘Submit2’ to access the value. I ended up getting the following error
Traceback (most recent call last):
File “/home/j.huang/Repos/ssa-code3_3/./pipeline/sensor_tasking/test.py”, line 75, in access_val
return ‘The input value was “{}”’.format(self.get_val())
^^^^^^^^^^^^^^^
File “/home/j.huang/Repos/ssa-code3_3/./pipeline/sensor_tasking/test.py”, line 49, in get_val
return self.objA.get_val2()
^^^^^^^^^^^^^^^^^^^^
File “/home/j.huang/Repos/ssa-code3_3/./pipeline/sensor_tasking/test.py”, line 22, in get_val2
return self.obj.val2
^^^^^^^^^^^^^
AttributeError: ‘ObjectB’ object has no attribute ‘val2’
According to the screen shot below, it seems that val2 once been set can’t be accessed by the subsequent callbacks.