I want to add callback to my bokeh select widget using CustomJS .I need to filter my dataframe according to callback from select widget. Since am a novice to JavaScript programming i havent been able to write the code for the callback
true_source=ColumnDataSource(data={'a':df.loc[, 'MONTH_YR'],
'b':df.loc[, 'MCOUNT'],
'qty':df.loc[,'MPDCQTY'],
'value':df.loc[,'MQTYRATE'],
'section':df.loc['MSECTION']})
#I added the true_source so I could have a reference from my original dataframe while filtering
source=ColumnDataSource(data={'x':df.loc[df['MSECTION']=='TURRET','MONTH_YR'],
'y':df.loc[df['MSECTION']=='TURRET', 'MCOUNT'],
'qty':df.loc[df['MSECTION']=='TURRET','MPDCQTY'],
'value':df.loc[df['MSECTION']=='TURRET','MQTYRATE']})
def callback_select(attr, old, new):
s=section.value
new_data={'x':df.loc[df['MSECTION']==s, 'MONTH_YR'],
'y':df.loc[df['MSECTION']==s, 'MCOUNT'],
'qty':df.loc[df['MSECTION']==s,'MPDCQTY'],
'value':df.loc[df['MSECTION']==s,'MQTYRATE']}
source.data=new_data
Since i want to deploy a HTML file, writing the callback in python wont work. So i tried writing the callback using CustomJS. But am sure i wrote it wrong.
callback=CustomJS(args=dict(source=source, ts=true_source), code=""""
var data1=ts.data;
var section=data1['section']
var a=data1['a']
var b=data1['b']
var data=source.data;
var f=cb_obj.value;
var x=[];
var y=[];
var j=0;
for(var i=0;i<section.length;i++){
if(section==f){
x[j].push(a[i]);
y[j].push(b[i]);
}
}
source.change.emit();
""")
this image here shows the plot, select option dosent work
Can anyone help me with this JS code?
xandy– and then immediately throwing them away. For them to have effect, you need to use them to setsource.datato be a new dict. It's not clear exactly what you are trying to do, so it's hard to be more specific. You almost certainly also wantif(section[i]==f)instead of what you have now.callback_selectwhich is the crucial bit: You need something likesource.data = new_databut you don't. You are never actually updatingsource, which is why nothing is changing.