Retrieve updated plot/frame width


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

from 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')];
dims.set('data', new_data);

def update_dims(attr, old, 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))


(taken from
to get the live frame width and height values. This solution is old so maybe there is a new and better way.



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