How to update editor values on Tabulator?

Hi,

I have been trying to update editor values for a Tabulator and haven’t been able to figure out even though I have spent six days for testing different kind of solutions to fix this.

Tabulator library has an a javascript example on how to update Tabulator selector values dynamically so I guess it should be possible also on Panel.

After creating a Tabulator I can change editor option values, but new option values don’t get rendered to the table.

Both of the following ways update option values:

table.editors['col1'].options = #list of values for options

table.editors.update(#dictionary of new editor objects)

Below is my full code for trying to figure out how to make this function properly. Is there something wrong with my code that I don’t understand which is causing SelectEditor options not updating on the Tabulator widget after updating the values?

I do appreciate any advice.

class TabuApp(pn.viewable.Viewer):
    listofoptions1 = ['A','B','C','D']
    listofoptions2 = ['E','T','X','U']
    
    this_class_bokeh_editors = {
        'col1': SelectEditor(options=listofoptions1),
        'col2': SelectEditor(options=listofoptions1),
        'col3': SelectEditor(options=listofoptions1),
    }
    
    this_class_other_bokeh_editors = {
        'col1': SelectEditor(options=listofoptions2),
        'col2': SelectEditor(options=listofoptions2),
        'col3': SelectEditor(options=listofoptions2),
    }

    def __init__(self):
        self.df = pd.DataFrame(columns=['col1', 'col2', 'col3'])
        self.table = pn.widgets.Tabulator(name ='table', value=self.df, show_index=False)
        self.table.editors.update(self.this_class_bokeh_editors)
        
    def __panel__(self):
        return self.table

    def fill_example_data(self):
        self.table.value = createTestDf()
        
    def change_editor_values(self):
        self.table.editors.update(self.this_class_other_bokeh_editors)
        
    def add_random_rows(self):
        new_df = pd.concat([self.table.value, createDfContent()], axis=0, verify_integrity=True)
        self.table._update_data(new_df)

                                        
def createTestDf():
    df = pd.DataFrame({'col1': ['D', 'Y'], 
                       'col2': ['NY', 'CA'], 
                       'col3': ['SVG', 'DVG']},
                  index=['XYSJ', 'JDKL'])
    return df

def createDfContent():
    df = pd.DataFrame({'col1': [giveRandomLetter(1), giveRandomLetter(1)], 
                       'col2': [giveRandomLetter(2), giveRandomLetter(2)], 
                       'col3': [giveRandomLetter(3), giveRandomLetter(3)]},
                  index=[giveRandomLetter(4), giveRandomLetter(4)])
    return df

def giveRandomLetter(length):
    letters = 'ABCDEFGHIJKLMNOPQR'
    outputstring = str()
    for i in range(0,length):
        randomvalue = randint(0,len(letters)-1)
        letter = letters[randomvalue]
        outputstring += letter
    return outputstring

Can you try replacing self.table.editors.update(...) by self.table.editors = dict(...). Modifying in place a container object like a Python dict or list doesn’t trigger any event at the Param level, this is how Param works. And what you really want in this case is to trigger an event, for Panel to know that you’ve asked for updating the editors Parameter value and that change should propagate to the right places.

1 Like

Thank you maximlt for your answer. Passing a dictionary solved this problem and it is also really useful to know that modifying objects doesn’t trigger an event to update the table. I have missed this while reading Panel documentation.

I am grateful for your help and happy to be able to continue building my Panel app.

1 Like