☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Trying to include js_on_change & callback in bokeh graph

Community Supportbrowsercustomjsbokehjswebsitebokeh
vinay_tiwari
vinay_tiwari

Hi Community , Greeting of the day.

I am trying to implement js_on_change on Pie graph but unable to do so . I am new to bokeh and JavaScript . Below is my code . So basically I want to implement a functionality that when ever I deselect any fruit , then my Pie chart should redraw itself by excluding that fruit from calculation .

My code is below .

from bokeh.models import ColumnDataSource,CustomJS,Slider
from bokeh.models import CheckboxGroup
from math import pi
import pandas as pd
from bokeh.io import show
from bokeh.plotting import figure
from bokeh.transform import cumsum 

from bokeh.palettes import Category10
fruits = {'Apple': 10,
          'Mango': 24,
          'Graphes': 9,
          'Banana': 15,
          'Peach': 6,
          'Pinapple' : 7,
    }

data = pd.Series(fruits).reset_index(name='value').rename(columns={'index':'Fruits'})
data['angle']=data['value']/data['value'].sum() * 2 *pi
data['color'] = Category10[len(fruits)]
data['percent']= data['value']/sum(fruits.values()) *100
p=figure(plot_height= 350 , title='Pie chart',tools = "hover",
         tooltips="@Fruits: @value  : @percent{0.2f} %", x_range=(-0.5,1.0))
p.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend='Fruits', source=data)
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color=None

callback =CustomJS(args = {'source':fruits},
code="""
var active=cb_obj.active;
const data = source;
const data1 = source;

var key1 = Object.keys(data)
var values1 = Object.values(data)
var new_fruit={}

var i=0
for (const [key,value] of Object.entries(data)) {
console.log(key,value);
console.log(active.includes(i));
var bool = active.includes(i);
if (bool==true)
  { console.log(active.includes(i));
    new_fruit[key]=value;

}
i=i+1
}


console.log("Callback completed");
console.log(active);
console.log(new_fruit);
"""
                  )
L = ["Apple","Mango","Graphes","Banana","Peach","Pinapple"]

checkbox_group = CheckboxGroup(labels=L, active =[0,1,2,3,4,5])
checkbox_group.js_on_change('active',callback)
layout = column(checkbox_group,p)

show(layout)
⌁   Link to post

End of archived discussion · Replies are disabled