Table Selections and Highlights

Data Type of Table Selections

You’ll get table selections using selections property as a SelectionRanges object. For instance, you opened a sample data

viewer.open_sample("iris")

and selected cells that correspond to df.iloc[0:100, 0:1] and df.iloc[2:3, 2:3], you’ll get following selection ranges.

table.selections
SelectionRanges([0:100, 0:1], [2:3, 2:3])

A SelectionRanges object is a list like object. Each selection is a tuple[slice, slice] object.

table.selections[0]
(slice(0, 100, None), slice(0, 1, None))

Thus, you can use them directly in iloc method of DataFrame.

table.data.iloc[table.selections[0]]

More simply, you can use values attribute to get values of selected table.

table.selections.values[0]
    sepal_length
0            5.1
1            4.9
2            4.7
3            4.6
4            5.0
..           ...
95           5.7
96           5.7
97           6.2
98           5.1
99           5.7

[100 rows x 1 columns]

To update selections, you can use list-like methods:

table.selections.append((slice(0, 10, None), slice(0, 1, None)))
table.selections.pop(0)

or directly set a list to selections.

table.selections = [(slice(0, 10, None), slice(0, 1, None))]

Catch Changes in Table Selections

You can bind a callback function that will get called on every selection change event. The events attribute is a SignalGroup object of psygnal. table.events.selections.connect() registers a callback function to table events.

@table.events.selections.connect
def _on_change(sel):
    print(sel)

Set Highlights

“Highlight” is another feature of a table. Similar to a selection, a highlight is also represented as a tuple of slices. Highlightened cells in a table are always painted.

Differences between a selection and a highlight are:

  • Highlights cannot be set as dynamically as selections. You have to convert selections into highlights from right-click context menu or programatically using attribute highlights.

  • Highlights do not vanish with mouse or keyboard operations.

  • Highlights do not emit signals.