Can someone please help with the following fragment of code? I want to update my tabulator dataframe based on the option selection in the comp column.
import datetime as dt
import numpy as np
import pandas as pd
import panel as pn
pn.extension('tabulator')
pn.extension(inline=True)
df = pd.DataFrame({
'nms': ['john', 'john', 'sara', 'tango'],
'comp':[['abc', 'def'], ['def'], ['ctf'], ['abc', 'def', 'ctf']]
})
filter_table = pn.widgets.Tabulator(df)
select1 = pn.widgets.MultiSelect(options=df.nms.unique().tolist())
filter_table.add_filter(select1, 'nms')
select2 = pn.widgets.MultiSelect(options=sorted(df.explode('comp').comp.unique().tolist()))
def contains_filter(event):
if select2.value==[]:
print("returning df...")
return df
else:
print(df.shape, select2.value[0])
return df[df.comp.apply(lambda x: True if select2.value[0] in x else False)]
# Watch for changes in the 'value' parameter of select2
select2.param.watch(contains_filter, 'value')
filter_table.add_filter(pn.bind(contains_filter))
# Reset button
reset_button = pn.widgets.Button(name='Reset Filters', button_type='warning')
# Define the reset function
def reset_filters(event):
select1.value = []
select2.value = []
# Attach the reset function to the button
reset_button.on_click(reset_filters)
# Layout with reset button and filters
layout = pn.Row(
pn.Column(select1, select2,reset_button),
filter_table,
)
layout.show()