Profile picture

Data Scientist @ RMOTR

Trash

Last updated: June 25th, 20202020-06-25Project preview
In [1]:
from bokeh.io import output_notebook, show
output_notebook()
Loading BokehJS ...
In [2]:
from bokeh.models import CustomJS, ColumnDataSource, MultiSelect, Row, Column, CheckboxGroup, Button
from bokeh.plotting import figure, show
import pandas as pd


data = dict(
    letter = ['A','A','B','C','B','B','A','C','C','B'],
    x = [1,2,3,4,5,6,7,8,9,10],
    y = [1,2,3,4,5,6,7,8,9,10]
)
data = pd.DataFrame(data)
 
data_source = ColumnDataSource(data)

active = [0, 1]
active_labels = pd.Categorical.from_codes(codes=active,
                                          dtype=pd.CategoricalDtype(list(data['letter'].unique())))
initial_data = data.loc[data.letter.isin(active_labels)]
initial_source = ColumnDataSource(initial_data)
 
plot = figure()
plot.circle('x', 'y', line_width = 2, source=initial_source)

checkboxes = CheckboxGroup(labels=list(data.letter.unique()), active=active)
 
callback = CustomJS(args={'source': initial_source, 'data_source': data_source,'checkboxes': checkboxes},
code = """
var data = data_source.data;
var x_data = data['x'];
var y_data = data['y'];
var letter = data['letter']
var letter = data['letter'];
 
var s_data = source.data;
var x = s_data['x'];
x.length = 0;
var y = s_data['y'];
y.length = 0;

var select_vals = checkboxes.active.map(function(i){return checkboxes.labels[i];}); 
var selected = checkboxes.active;

for (var i = 0; i < x_data.length; i++) {
    if (select_vals.indexOf(letter[i])>=0) {
        x.push(x_data[i]);
        y.push(y_data[i]);
    }
}

source.change.emit(); 
""")
 
b1 = Button(label="clear all")
b1.js_on_click(CustomJS(args=dict(checkboxes=checkboxes), code="""
    checkboxes.active = []
"""))
 
b2 = Button(label="select all")
b2.js_on_click(CustomJS(args=dict(checkboxes=checkboxes), code="""
    checkboxes.active = [0,1,2,3,4,5,6,7,8,9,10,11]
"""))

checkboxes.js_on_change('active', callback)

layout = Row(Column(b1,b2,checkboxes), plot)
 
show(layout)
In [ ]:
 
Notebooks AI
Notebooks AI Profile20060