Dear developers,
I am recently into the Dash & Ploty community, and love the interactive app a lot! I am used to object-oriented programming and hope to take advantage of the class architecture to simplify the coding. I’ve read around and given a few attempts. Hope to share what I know and get advice if anyone happens to know how to do object-oriented programming for callbacks. Thanks in advance!
from dash import Dash, dcc, html, Input, Output
class App(Dash):
def __init__(self, *arg, **kwarg):
super().__init__(*arg, **kwarg)
self.layout = html.Div([
html.
H6("Change the value in the text box to see callbacks in action!"),
html.Div([
"Input: ",
dcc.Input(id='my-input', value='initial value', type='text')
]),
html.Br(),
html.Div(id='my-output'),
])
self.callback(
Output(component_id='my-output', component_property='children'),
Input(component_id='my-input',
component_property='value'))(self.update_output_div)
def update_output_div(self, input_value):
return f'Output: {input_value}'
if __name__ == '__main__':
app = App(__name__)
app.run_server(debug=True)
The above code works, but the callback must be called inside the init method away from the def update_output_div method. Can help one help to make something similar to the following work?
@Dash.callback(
Output(component_id='my-output', component_property='children'),
Input(component_id='my-input',
component_property='value'))
def update_output_div(self, input_value):
return f'Output: {input_value}'
self._callback_list and self.callback_map in def callback method stops me from achieving it. I am not sure how much work would be put into to get rid of the local copy and utilize the global GLOBAL_CALLBACK_LIST and GLOBAL_CALLBACK_MAP directly? Possible or not? Or other workarounds?
Sincerely,
Teng