Plots do not update interactive annotations as expected (can't add them to the plot)

Hello!
In this pet project of mine, I would like to add some interactive annotations in a graph, that is, selecting ranges through text boxes and showing them on a Figure upon clicking a button.
Seems to me I have done like the sample code in the docs say I should, but still not working as expected.

Sample code that reproduces the issue for review at GitHub - srp0706/bokehplay: A minimal example of a difficulty I'm having with a pet project..
Not sure if related or not, but the code was written using Bokeh 1.0.4 and did not have the “Sync Axes” feature. When I added that, the annotations stopped working. Then I upgraded to Bokeh 1.2.0 to check if it would be fixed, but it did not solve the issue. I then removed the new code, reverting to the old one (only annotations, no sync axes), and it still did not do the trick. What am I missing here?

Your insights are much appreciated.

Best regards!

I would recommend adding the BoxAnnotation and Span up front, but possibly making them invisible until you want them to be seen. (The callback can update the data for these existing annotations, rather than create and add them itself) This is generally good advice. The very best thing usage Bokeh is optimized and which has been exercised by far the most is updating data, including properties of existing objects. Adding/removing entire objects is a much more complicated operation and may or may not work, depending (some things are only hooked up once, at initialization, for instance),.

Thanks for the quick response Bryan, that did work.
However that prompts another question: what if I want to add two spans (or more)? I would think perhaps the Span isn’t the way to go, unless it’s possible to somehow add a list of values to a single Span renderer and update this list interactively. Other thoughts/recommendations/advice?

Do you have an idea up front how many spans you might need up front? If so I would just create them all and manage them as needed. In my mind annotations like these are things there are only a few of, but maybe that’s a failure of my imagination. In any case if that is not an option, then:

  • can use regular data-source driven glyphs like line (or segment or ray) and rect. Some drawbacks but usually serviceable (short term)
  • make adding/subtracting annotations dynamically in server apps more robust (longer term)

Thanks again for the reply, Bryan.

Unfortunately I don’t know how many Spans I will need at plot construction.
I changed the code to use Segments as you suggested and it worked beautifully.

1 Like