I am trying to create a pie chart in a jupyter notebook with Bokeh that can be updated with a slider. I have a custom function that creates data from a pre-existing dataframe. I would like the slider to manipulate input f to that function, such that data is different when displayed in the pie graph. Here is an example:
vals = [20, 100, 50]
names = ["Agnostic", "Competetive", "Loyalist"]
def data_generator(names, vals, f):
data = pd.DataFrame([[name, val * f] for name, val in zip(names, vals)])
return data
data = data_generator(names, vals, 10)
data['angle'] = data['value']/data['value'].sum() * 2*pi
data['color'] = Plasma[len(data)]
p = figure(plot_height=350, title="Loyalty Breakout", toolbar_location=None,
tools="hover", tooltips="@segment: @value", 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_field='segment', source=data)
# This is where I need help
update = CustomJS(args=dict(xr=p.x_range), code="""
// I got this JS code from somewhere else. I need this to update the 'f' value in my input funciton.
// JavaScript code goes here
var a = 10;
// the model that triggered the callback is cb_obj:
var f = cb_obj.value;
// models passed as args are automagically available
xr.start = a;
xr.end = b;
""")
lookback_slider = Slider(start=0, end=180, value=30, step=1, title="Lookback (days)")
lookback_slider.js_on_change('value',update)
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None
show(column(lookback_slider, p))
This creates the pie chart and slider, but the slider does not work. I found this: Using bokeh to plot interactive pie chart in Jupyter/Python but unfortunately, CustomJS.from_py_func(update) won't work because from_py_func has been deprecated in the newest version of Bokeh. Can anyone help me update a pie chart with Bokeh?