How to use a slider callback to filter a ColumnDataSource in Bokeh using Python 3?

I’m trying to use a slider with a callback in Bokeh using Python 3 to filter the rows of my ColumnDataSource objects (which originate from a DataFrame). More specifically, if a slider with options of 0 to 10000000 (in multiples of 1 million) returns a value N of say 2000000, then I want my plot to only show the data for, in this case, US counties where the population is >= 2000000. Below is my code. Everything works as I want it to except for the slider callback.

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import HoverTool, ColumnDataSource, Select, Slider
from bokeh.plotting import figure
TOOLS='pan,wheel_zoom,box_zoom,reset,tap,save,box_select,lasso_select'

source1 = ColumnDataSource(df[df.winner == 'Democratic'])
source2 = ColumnDataSource(df[df.winner == 'Republican'])

hover = HoverTool(
        tooltips = [
            ('County Name', '@county'),
            ('Population', '@population'),
            ('Land Area', '@land_area'),
            ('Pop. Density', '@density'),
            ('Winning Party', '@winner'),
            ('Winning Vote %', '@winning_vote_pct'),
            ]
        )

# Plot
plot = figure(plot_width=800, plot_height=450, tools=[hover, TOOLS],
           title='2016 US Presidential Vote % vs. Population Density (by County)',
           x_axis_label='Vote %', y_axis_label='Population Density (K / sq. mi.)')

y = 'density'
size = 'bokeh_size'
alpha = 0.5

c1 = plot.circle(x='pct_d', y=y, size=size, alpha=alpha, color='blue',
            legend='Democratic-Won County', source=source1)
c2 = plot.circle(x='pct_r', y=y, size=size, alpha=alpha, color='red',
            legend='Republican-Won County', source=source2)

plot.legend.location = 'top_left'

# Select widget
party_options = ['Show both parties', 'Democratic-won only', 'Republican-won only']
menu = Select(options=party_options, value='Show both parties')

# Slider widget
N = 2000000
slider = Slider(start=0, end=10000000, step=1000000, value=N, title='Population Cutoff')

# Select callback
def select_callback(attr, old, new):
    if menu.value == 'Democratic-won only': c1.visible=True; c2.visible=False
    elif menu.value == 'Republican-won only': c1.visible=False; c2.visible=True
    elif menu.value == 'Show both parties': c1.visible=True; c2.visible=True
menu.on_change('value', select_callback)

# Slider callback
def slider_callback(attr, old, new):
    N = slider.    value
# NEED HELP HERE...
    source1 = ColumnDataSource(df.loc[(df.winner == 'Democratic') & (df.population >= N)])
    source2 = ColumnDataSource(df.loc[(df.winner == 'Republican') & (df.population >= N)])
slider.on_change('value', slider_callback)

# Arrange plots and widgets in layouts
layout = layout([menu, slider],
                [plot])

curdoc().add_root(layout)

Came across your post via google as I’m trying to do something similar. It appears that Slider.on_change only works with bokeh-server as is briefly explained here Adding Widgets. You’ll probably need to do a CustomJS callback instead unless you are in fact using bokeh-server.

···

On Tuesday, September 26, 2017 at 6:34:11 AM UTC-7, Scott Pritchard wrote:

I’m trying to use a slider with a callback in Bokeh using Python 3 to filter the rows of my ColumnDataSource objects (which originate from a DataFrame). More specifically, if a slider with options of 0 to 10000000 (in multiples of 1 million) returns a value N of say 2000000, then I want my plot to only show the data for, in this case, US counties where the population is >= 2000000. Below is my code. Everything works as I want it to except for the slider callback.

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import HoverTool, ColumnDataSource, Select, Slider
from bokeh.plotting import figure
TOOLS='pan,wheel_zoom,box_zoom,reset,tap,save,box_select,lasso_select'

source1 = ColumnDataSource(df[df.winner == 'Democratic'])
source2 = ColumnDataSource(df[df.winner == 'Republican'])

hover = HoverTool(
        tooltips = [
            ('County Name', '@county'),
            ('Population', '@population'),
            ('Land Area', '@land_area'),
            ('Pop. Density', '@density'),
            ('Winning Party', '@winner'),
            ('Winning Vote %', '@winning_vote_pct'),
            ]
        )

# Plot
plot = figure(plot_width=800, plot_height=450, tools=[hover, TOOLS],
           title='2016 US Presidential Vote % vs. Population Density (by County)',
           x_axis_label='Vote %', y_axis_label='Population Density (K / sq. mi.)')

y = 'density'
size = 'bokeh_size'
alpha = 0.5

c1 = plot.circle(x='pct_d', y=y, size=size, alpha=alpha, color='blue',
            legend='Democratic-Won County', source=source1)
c2 = plot.circle(x='pct_r', y=y, size=size, alpha=alpha, color='red',
            legend='Republican-Won County', source=source2)

plot.legend.location = 'top_left'

# Select widget
party_options = ['Show both parties', 'Democratic-won only', 'Republican-won only']
menu = Select(options=party_options, value='Show both parties')

# Slider widget
N = 2000000
slider = Slider(start=0, end=10000000, step=1000000, value=N, title='Population Cutoff')

# Select callback
def select_callback(attr, old, new):
    if menu.value == 'Democratic-won only': c1.visible=True; c2.visible=False
    elif menu.value == 'Republican-won only': c1.visible=False; c2.visible=True
    elif menu.value == 'Show both parties': c1.visible=True; c2.visible=True
menu.on_change('value', select_callback)

# Slider callback
def slider_callback(attr, old, new):
    N = slider.    value
# NEED HELP HERE...
    source1 = ColumnDataSource(df.loc[(df.winner == 'Democratic') & (df.population >= N)])
    source2 = ColumnDataSource(df.loc[(df.winner == 'Republican') & (df.population >= N)])
slider.on_change('value', slider_callback)

# Arrange plots and widgets in layouts
layout = layout([menu, slider],
                [plot])

curdoc().add_root(layout)