Is there a way to patch multiple pattern-matched elements in a single callback?
For example:
import numpy as np
from dash import Patch, Input, Output, State
@app.callback(
Output({'type': 'plot', 'index': ALL}, 'figure'}
Input('trigger', 'data'}
State({'type': 'plot', 'index': ALL}, 'figure'})
)
def update_plot_title_colors(trigger, figs):
num_figs = len(figs)
rgb_colors: [list(np.random.choice(range(256), size=3)) for _ in figs]
patches = [] # what is the correct syntax for multiple patches?
for rgb in rgb_colors:
color = f"rgb({rgb[0]}, {rgb[1]}, {rgb[2]})"
patched_figure = Patch() # should location or parent be specified?
patched_figure["layout"]["title"]["font"]["color"] = color
patches.append(patched_figure)
return patches
What is the correct syntax to patch multiple elements? Is this sort of functionality even supported?
Thanks for the welcome! I’m not sure what I did wrong before, but this does in fact work. I determined the best way to execute this type of callback is as follows:
import numpy as np
from dash import Patch, Input, Output, State
from dash.exceptions import PreventUpdate
@app.callback(
Output({'type': 'plot', 'index': ALL}, 'figure'}
Input('trigger', 'data'}
State({'type': 'plot', 'index': ALL}, 'id'})
)
def update_plot_title_colors(trigger, plot_ids):
if not plot_ids:
raise PreventUpdate
rgb_colors =[list(np.random.choice(range(256), size=3)) for _ in plot_ids]
patches = []
for rgb in rgb_colors:
color = f"rgb({rgb[0]}, {rgb[1]}, {rgb[2]})"
patched_figure = Patch()
patched_figure["layout"]["title"]["font"]["color"] = color
patches.append(patched_figure)
return patches
Loading the State(component_proprty='id') is a better way to determine how many patches are needed (and potentially manipulate specific figures differently depending on their 'index') without the overhead of loading the entire figure.