Skip to content Skip to sidebar Skip to footer

Bokeh: Using Checkbox Widget To Hide And Show Plots

I have some difficulties to use the Widget callback with Bokeh. With the help of a checkbox widget, I would like to show/hide the corresponding plots. The difference with this ques

Solution 1:

It is easier to realise it using Python on_click handler instead of CustomJS callback:

from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import CheckboxGroup
from bokeh.plotting import curdoc, figure
from bokeh.client import push_session
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4), columns = ['a', 'b', 'c', 'd'])
source = ColumnDataSource(df)

def checkbox_click_handler(selected_checkboxes):
    visible_glyphs = layout.children
    for index, glyph in enumerate(glyph_list):
        if index in selected_checkboxes:
            if glyph not in visible_glyphs:
                layout.children.append(glyph)
        else:
            if glyph in visible_glyphs:
                layout.children.remove(glyph)

checkbox_group = CheckboxGroup(labels = list(df.columns.values), active = [0, 1, 2, 3, 4])
checkbox_group.on_click(checkbox_click_handler)

layout = column()
layout.children.append(checkbox_group)

glyph_list = []
for index, letter in enumerate(df.columns.values):
    glyph = figure(plot_width = 800, plot_height = 240, title = letter, name = letter)
    glyph.circle(source = source, x = 'a', y = letter)
    glyph_list.append(glyph)
    layout.children.append(glyph)

session = push_session(document = curdoc())
session.show(layout)
session.loop_until_closed()

Post a Comment for "Bokeh: Using Checkbox Widget To Hide And Show Plots"