1
votes

I have a pandas DataFrame similar to the one below

import pandas as pd
import numpy as np
df = pd.DataFrame(columns=['item'], data=list(range(0,15)))
df['crit'] = ['crit1','crit2','crit3']*5
df['label'] = np.random.choice(['label1','label2','label3'], size=(15,1))
df.head()

enter image description here

I am plotting a Bar chart with Bokeh

from bokeh.io import output_file, show, output_notebook
from bokeh.charts import Bar
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import CheckboxButtonGroup

source = ColumnDataSource(df)
p = Bar(df, label='label', values='item', agg='count', legend=None)
checkbox = CheckboxButtonGroup(labels=["Opt1", "Opt2", "Opt3"], active=[0, 1])
grid = layout([[p], [checkbox]])
show(grid)

enter image description here

I would like to add a custom JS callback that filters and sort the dataframe to update the plot. For example by clicking on the button 'Opt2' I would like to filter the dataframe to only keep rows where crit==crit2 and I would like the dataframe to be sorted (to have the bars in decreasing order).

I am not familiar with JS, only Python is my friend... Is it something easily achievable? Could someone put me in the track? The example I found on the bokeh documentation did not correspond to this use case.

1

1 Answers

1
votes

Follow this bokeh gallery example (IMDB movies): https://demo.bokeh.org/movies

Its code is here:

https://github.com/bokeh/bokeh/blob/master/examples/app/movies/main.py

Last part does exactly what you want, look into this function:

def select_movies():