Arbitrary Renderer allowed in Legend?

I like the fact that I can use the legend to toggle lines / points on and off. This is really convenient.

When I tried to also use the legend to toggle BoxAnnotations on/off by adding them to the figure.legend so I could click on them, I got the following error:

error handling message Message ‘PATCH-DOC’ (revision 1): ValueError("expected an element of List(Instance(GlyphRenderer)), got seq with invalid items [BoxAnnotation(id=‘652d8363-2e2a-4ca9-ba98-343054754ff2’, …), BoxAnnotation(id=‘9f4a8e05-83ad-4dad-adac-2268ef19f1c7’, …)


Looking into it further, this approach might not work since BoxAnnotation subclasses Annotation which subclasses Renderer instead of GlyphRenderers… This means that BoxAnnotations and GlyphRenderers tree up through different branches to the base Renderer class. As such it seems that the Legend in its current form can only turn on and off Glyphs?

Thanks for any help or advise you may be able to provide.



For reference, I was trying to do something like the following and it failed on the “legend_item = LegendItem” line with the error above.

f = figure(title='some title')
f.line(x='x', y='y', source=source, legend="my line")
box_annotation_list = some_function_which_gives_me_box_annotations()
legend_item = LegendItem(label="new legend item", renderers=box_annotation_list)
# this might not be right since .items is a List()

Note that I got around this by adding a MultiSelect() to the left of the plot and as the user selects various options, it shows / hides the associated BoxAnnotation on the plot. While this works, it would be really nice if there was a means to add any Renderer to the legends so we could use the functionality in the legend to show/hide any renderer on the plot as long as we list the Renderer in the legend.

Would this be a feature request on GitHub? would this be easy to do, or would it require significant structural changes to how legends work?