Filter unique values operation is not being carried out on the datatable’s column during live operation.On filtering the datatable doesnot update to show only the filtered value’s respective rows.
Is it possible to do filtering directly in python or should one use CustomJs?
CODE:
from bokeh.io import save
from bokeh.embed import file_html
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, DataTable, TableColumn, CheckboxEditor, Select
import pandas as pd
# Sample data
data = {
'Category': ['A', 'A', 'B', 'B', 'B', 'C', 'C'],
'Value': [10, 20, 15, 25, 30, 5, 10],
'Checked': [True, False, True, False, True, False, True]
}
df = pd.DataFrame(data)
# Create a ColumnDataSource from the DataFrame
source = ColumnDataSource(df)
# Define columns for the DataTable
columns = [
TableColumn(field='Category', title='Category'),
TableColumn(field='Value', title='Value'),
TableColumn(field='Checked', title='Checked', editor=CheckboxEditor())
]
# Create the DataTable
data_table = DataTable(source=source, columns=columns, width=400, height=280, index_position=None)
# Create Select widget to filter Category
category_filter = Select(title="Filter Category:", options=sorted(df['Category'].unique()), value=None)
# Callback function to filter data based on selected category
def filter_data(attr, old, new):
if new:
new_data = df[df['Category'] == new]
source.data = ColumnDataSource(new_data).data
else:
source.data = ColumnDataSource(df).data
category_filter.on_change('value', filter_data)
# Create a layout
layout = column(category_filter, data_table)
# Save the layout as an HTML file
html_output = file_html(layout, title="My Bokeh App")
with open("output.html", "w") as f:
f.write(html_output)