Interactively subset AND edit dataframes with panel's tabulator

198 views Asked by At

I want to interactively edit a large dataframe, by also interactively subsetting to batches of rows. I am trying this with the Python package panel and its Tabulator class in a jupyter lab notebook. So far, no success.

Where I am right now:

  • A tutorial covers interactive editing (without interactive subsetting) and works fine.
  • I was also able to interactively subset the table using the panel.bind method and a Select widget.

However, when I try to combine both interactions, I fail: a single interactive edit results in disrupting the interactive subsetting functionality.

Here is a reproducible example from inside a jupyter lab notebook:

(panel==1.3.1, pandas==2.1.3)

import pandas as pd
import panel as pn
from bokeh.models.widgets.tables import SelectEditor

pn.extension('tabulator')
df = pd.DataFrame([
    ['a', 'Hello', 0, 'keep'],
    ['b', 'Hi', 0, 'keep'],
    ['c', 'World', 0, 'keep'],
    ['d', 'Planet', 1, 'keep'],
    ['e', 'Earth', 1, 'keep']
], columns=['id', 'name', 'predicted', 'decision'])

FYI: The predict column is results from clustering similar names. Now I want to manually review the results and re-label if I spot errors.

# Create list of subsets of the data:
dfs = [df[df.predicted.eq(c)] for c in (0, 1)]

# Use the Select widget to switch between the subsets
select = pn.widgets.Select(name='Cluster #', value=0, options=list(range(len(dfs))))

# Create an interactive table, returning the subset based on the select widget:
def table_creator(cluster):
    return dfs[cluster]

interactive_table = pn.bind(table_creator, select)

# Use the interactive table as the data inside the Tabulator widget:
tabulator = pn.widgets.Tabulator(
    interactive_table, 
    editors={'decision': SelectEditor(options=['keep', 'exclude']),
             'id': None, 'name': None, 'predicted': None}
)

# Display Select and Tabulator widgets side by side:
pn.Column(
    select,
    tabulator
)

Running the last cell gives me an interactive view on the subsets of the data:

Image of interactive output from last cell

And I can switch without problems between the two subsets. However, after the very first cell edit, switching between the two datasets is no longer possible.

Any idea how to resolve this issue, or any idea for a workaround? At the end of the day, I want to manually review and potentially re-label batches of the data.

0

There are 0 answers