column data source update on callback

Hi,

I want to update a column data source from a dictionary defined outside the callback function but couldn’t do so. I was following weather application on this link and as I understand, it is possible that we can achieve this in callback functions.

This function shows that we can use variable defined outside the callback function

def update_plot(attrname, old, new):
city = city_select.value
plot.title.text = "Weather data for " + cities[city][‘title’]
src = get_dataset(df, cities[city][‘airport’], distribution_select.value)
source.data.update(src.data)

However, when I try to update column data source of a multi line glyph, it is giving me error, saying the dictionary that I try to update values with is not defined which is understandable as callback function is run on browser side but how about weather example then? I think it is possible to update with some variables defined outside the function scope but how? Here is the code I try to run.

from bokeh.plotting import figure, output_file, show

from bokeh.models import ColumnDataSource, CDSView

from bokeh.models import CustomJS,ColumnDataSource,Slider,Plot,DataRange1d,HoverTool,Button,Legend

from bokeh.plotting import figure, curdoc

from bokeh.layouts import row,column

p = figure(plot_width=400, plot_height=400)

source = ColumnDataSource(data=dict(x=,

y=,

color=,

alpha= ))

some_dic = {“x” : [[1,1],[1,1]],“y” : [[200,100],[100,0]],“color” : [“firebrick”,“navy”],“alpha”: [0.8,0.3]}

p.multi_line(xs=‘x’,ys=‘y’,line_color=‘color’,alpha = ‘alpha’,line_width=4,source=source)

def callbackPatternSelect(source = source):

source.data.update(some_dic)

#or source.data = some_dic

#source.change.emit()

p.js_on_event(‘tap’,CustomJS.from_py_func(callbackPatternSelect))

#put the button and plot in a layout and add to the document

curdoc().add_root(column(p))

Thanks.

I think it is because tap not working properly on figures but only on glyph right? So it would work if button and on_click have been used instead.

···

On Monday, May 14, 2018 at 2:56:30 PM UTC+3, Baran Köseoğlu wrote:

Hi,

I want to update a column data source from a dictionary defined outside the callback function but couldn’t do so. I was following weather application on this link and as I understand, it is possible that we can achieve this in callback functions.

This function shows that we can use variable defined outside the callback function

def update_plot(attrname, old, new):
city = city_select.value
plot.title.text = "Weather data for " + cities[city][‘title’]
src = get_dataset(df, cities[city][‘airport’], distribution_select.value)
source.data.update(src.data)

https://github.com/bokeh/bokeh/tree/master/examples/app/weather

However, when I try to update column data source of a multi line glyph, it is giving me error, saying the dictionary that I try to update values with is not defined which is understandable as callback function is run on browser side but how about weather example then? I think it is possible to update with some variables defined outside the function scope but how? Here is the code I try to run.

from bokeh.plotting import figure, output_file, show

from bokeh.models import ColumnDataSource, CDSView

from bokeh.models import CustomJS,ColumnDataSource,Slider,Plot,DataRange1d,HoverTool,Button,Legend

from bokeh.plotting import figure, curdoc

from bokeh.layouts import row,column

p = figure(plot_width=400, plot_height=400)

source = ColumnDataSource(data=dict(x=,

y=,

color=,

alpha= ))

some_dic = {“x” : [[1,1],[1,1]],“y” : [[200,100],[100,0]],“color” : [“firebrick”,“navy”],“alpha”: [0.8,0.3]}

p.multi_line(xs=‘x’,ys=‘y’,line_color=‘color’,alpha = ‘alpha’,line_width=4,source=source)

def callbackPatternSelect(source = source):

source.data.update(some_dic)

#or source.data = some_dic

#source.change.emit()

p.js_on_event(‘tap’,CustomJS.from_py_func(callbackPatternSelect))

#put the button and plot in a layout and add to the document

curdoc().add_root(column(p))

Thanks.