I have a multi choice widget in which the options are columns in a data table. When the option is selected I would like to show those columns in the data table and hide the rest of the columns. Here is my code so far
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool,CustomJS, Div, Legend, LegendItem,DatetimeTickFormatter, CDSView, GroupFilter,TabPanel, Tabs, MultiChoice,DateRangePicker
from bokeh.models.widgets import Select, DataTable, TableColumn, HTMLTemplateFormatter, FileInput
from bokeh.layouts import row, column, layout
from bokeh.transform import factor_cmap, factor_mark
df=pd.DataFrame({"Li":[10,20,30,40,50,60,70,80,90,100],"Be":[1,2,3,4,5,6,7,8,9,10],"Sc":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]})
filtered_table_data=df[["Li","Be"]]
filtered_table_source= ColumnDataSource(data=filtered_table_data)
filtered_table_cols=[]
filtered_table_cols.append(TableColumn(field='Li', title='Li', width=750, visible=False))
filtered_table_cols.append(TableColumn(field='Be', title='Be', width=750,visible=False))
filtered_table=DataTable(source=filtered_table_source, columns=filtered_table_cols)
multi_choice = MultiChoice(value=[], options=df.columns[:-1].tolist(), title='Select elements:')
callback2 = CustomJS(args=dict(multi_choice=multi_choice, filtered_table=filtered_table), code="""
for (var i=0; i<filtered_table.columns.length; i++)
{
for (var j=0; j<multi_choice.value.length;j++)
{
if (filtered_table.columns[i].field==multi_choice.value[j])
{
filtered_table.columns[i].visible=True
}
}
}
""")
multi_choice.js_on_change("value",callback2)
l2=layout([multi_choice, filtered_table])
show(l2)
In this example what I would like to happen is if Be was selected with the multi choice widget then only the column Be would be visible in the data table. So far nothing happens when I select options with the multi choice widget
Posted question on Stack Overflow: javascript - Bokeh CustomJS callback to change data table column visibility to True - Stack Overflow