Hi all
I’m trying to write more efficient and scalable/reusable code using the Multiplexer plugin, and pattern-matching callbacks. As previously reported, the MATCH selector is not compatible with the Multiplexer feature. I’m wondering if there is another elegant & efficient method to reuse code.
My current situation is the following: I have a quite big multi-page dash App. I have a Plotly graph in which my data is visualized. The data for the graph is selected out of Table, and below the graph I have div where you can select layout options (=a layout editor for the graph). Currently, I have two functions ( data_selection() and layout_editor()), both have the graph as output, which works because of the Multiplexer plugin. However, I would like to reuse the layout-editor on other pages of my app as well.
Currently, every time I want to use this layout-editor again for a graph I have to copy the corresponding layout-editor_callbacks.py file, and edit all the id’s to match the new graph. However, when I change something to the layout-editor I have to go through each instance of this copied layout-editor and update the corresponding callbacks. Normally, such a scenario can be avoid using the MATCH selector (pattern-matching callbacks). However, this is not possible if more than one callback has the same output (i.e. the graph is the outputs of multiple callbacks).
I have been searching for a solution for quite some time now, but the only thing I have found was this post on stackoverflow where it is suggested to use the ALL selector (in combination with callback_context) rather than the MATCH selector, as the former is compatible with the Multiplexer feature. This still feels very inconvenient, as once you have more than a few instances this becomes a very big decision tree. (see below for a minimalistic example).
@callback(
Output({'type':'graph_scatter', 'index':'page1'}, 'figure'),
Output({'type':'graph_scatter', 'index':'page2'}, 'figure'),
....
Output({'type':'graph_scatter', 'index':'page10'}, 'figure'),
Input({"type":"fig_height","index":dash.ALL}, 'value'),
Input({"type":"fig_width","index":dash.ALL}, 'value'),
State({'type':'graph_scatter', 'index':dash.ALL}, 'figure')
)
def update_figure_size(fig_height, fig_width, fig):
ctx = callback_context.triggered_id
if ctx == "page1":
return(update_fig_size(),no_update, no_update, ...., no_update)
elif ctx == "page2":
return(no_update,update_fig_size(), no_update, ...., no_update)
....
Any workarounds or tips would be very helpful.
Thanks in advance!