2

I have a Bokeh plot that can display an image, which can be changed by a slider (and also should update regularly) (similar to this question Sliding through images with Bokeh Slider). So the url of the image changes and Bokeh reloads the image. Is there a way to attach a callback function to the load of the image?

Community
  • 1
  • 1
Randrian
  • 1,055
  • 12
  • 25

2 Answers2

2

As of Bokeh 1.4.0 there is no callback on the image load. The ImageURL glyph is not one that is much used by the core developers, AFAIK, and we can't always anticipate every use case that every user might want. For this reason, we have made Bokeh itself extensible, so the best I can suggest at the moment is to extend Bokeh with a custom extension. The documentation for creating custom extensions is here:

http://docs.bokeh.org/en/latest/docs/user_guide/extensions.html

In abbreviated forms, you'd create a class like this that adds a callback property, and supplies the JavaScript implementation for your custom ImageURL glyph:

class MyImageURL(ImageURL):

     callback = Instance(Callback, help="A callback to run when images load")

    __implementation__ = JAVASCRIPT_CODE_HERE

Then it can be used like any low level glyoh:

my_glyph = MyImageURL(...)
source = ColumnDataSource(...)
plot.add_glyph(source, my_glyph)
bigreddot
  • 33,642
  • 5
  • 69
  • 122
0

You can subscribe to the change event of the image tag and do whatever you need to. You can do this with JQuery's change method or straight javascript's onchange handler.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Well, Bokeh doesn't create an img tag in the html tree, I just found a canvas tag. I suppose that Bokeh renders the image on this canvas tag. – Randrian Nov 19 '16 at 18:49
  • It was just a suggestion to point you in that train of thought. You can do that for any tag that Bokeh uses. – CodingYoshi Nov 19 '16 at 18:51
  • But then I have to distinguish between changes of the image or other change to the plot (e.g. pan or zoom). – Randrian Nov 19 '16 at 19:06
  • I looked at the API and it has many callbacks, one is OpenUrl as shown here http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html. Will that help at all? Am I totally looking at the wrong thing? – CodingYoshi Nov 19 '16 at 19:19
  • No, that is a callback to open a different URL in the browser tab, not when Bokeh loads an image from a different url. I have already been on that page, but unfortunately I didn't find a callback. – Randrian Nov 19 '16 at 19:38