Selecting only one row in Tabulator widget

Hi,
I want to build an application where the user can select a record from a table and then depending on the chosen record a plot will be shown. This only makes sense if only one row is selected, so if the user selects another row, the previously selected row must be unselected.
I use the Tabulator widget do display the table, and reading the docs I thought that was default behaviour, and you would have to use ctrl/shift-click for multiselect. This seems not to be the case though.

I tried to unselect the old selection manually, but the tabular keeps the old selection highlighted and the displayed single row is not updated correctly. Does anyone know how to archive this?

import param
import panel as pn
import pandas as pd
import numpy as np

#pn.extension('perspective')
pn.extension(comms='vscode')

class ReactiveTables(param.Parameterized):
    
    def __init__(self, df):
        super().__init__()
        self.dataset = df
        self.tabulator = pn.widgets.Tabulator(df, width = 1200, height=700, disabled = True, pagination = 'remote', show_index = False, layout='fit_data_stretch', theme = 'site', selectable = True)
        self.row_pane = pn.panel(self.dataset.loc[self.tabulator.selection], width = 800, height=600)
        self.updating_selection = False

    def get_row(self, selection):
        print('old: {}, new: {}'.format(selection.old, selection.new))
        
        if self.updating_selection:
            return
        self.updating_selection = True
        diff = list(set(selection.new) - set(selection.old))
        print(diff)
        self.tabulator.selection = diff        
        self.row_pane.object = self.dataset.loc[self.tabulator.selection]

        self.updating_selection = False
   
    def panel(self):
        link = self.tabulator.param.watch(self.get_row, 'selection')        
        return pn.Row(self.tabulator, self.row_pane)
    
my_df = pd.DataFrame({"a": [1,1,1,2,2,2,3,3,3], "b": [4,4,4,5,5,6,5,6,6]})

reactive = ReactiveTables(my_df)
reactive.panel().servable()

The solution is to pass
selectable = 1
in the ctor. This option is not documented in the panel doc for tabulator, but you can find it in the doc of tabulator itself.

2 Likes

Glad you found it @jleoff! Could you file an issue about explicitly supporting that feature?

Sure, here it is: https://github.com/holoviz/panel/issues/2089

1 Like