Retrieve updated plot/frame width

Hi,

I would like to know if there is a better way than this

from bokeh.io import curdoc
from bokeh.models import CustomJS, ColumnDataSource
import numpy

from bokeh.plotting import Figure

jscode = """
var new_data = {};
new_data['height'] = [plot.get('frame').get('height')];
new_data['width'] = [plot.get('frame').get('width')];
console.log(new_data);
dims.set('data', new_data);
"""

def update_dims(attr, old, new):
    print(new)

x = numpy.linspace(0, 10, 100)
y = x + numpy.random.random(100)

p = Figure(x_range=(0, 10), y_range=(0, 100))

dims = ColumnDataSource(data=dict(width=[], height=[]))
dims.on_change('data', update_dims)

p.x_range.callback = CustomJS(code=jscode, args=dict(plot=p, dims=dims))

curdoc().add_root(p)

(taken from Expose frame height/width to Bokeh server · Issue #3747 · bokeh/bokeh · GitHub)
to get the live frame width and height values. This solution is old so maybe there is a new and better way.

Thanks

Yes:

plot.on_change('inner_width', ...)
plot.on_change('inner_height', ...)

Also note: almost all the old .callback properties are deprecated and will be removed in Bokeh 2.0, you should use the general on_change / js_on_change methods as above.

1 Like