diff --git a/panel/tests/widgets/test_tables.py b/panel/tests/widgets/test_tables.py index 2274efb9d7..5e086b5d16 100644 --- a/panel/tests/widgets/test_tables.py +++ b/panel/tests/widgets/test_tables.py @@ -336,7 +336,7 @@ def test_tabulator_expanded_content_pagination(document, comm): assert len(model.children) == 0 -def test_tabulator_expanded_content_embed(document, comm): +def test_tabulator_content_embed(document, comm): df = makeMixedDataFrame() table = Tabulator(df, embed_content=True, row_content=lambda r: r.A) @@ -358,6 +358,34 @@ def test_tabulator_expanded_content_embed(document, comm): assert row.text == f"<pre>{r.A+1}</pre>" +def test_tabulator_content_embed_and_expand(document, comm): + # https://github.com/holoviz/panel/issues/6200 + df = makeMixedDataFrame() + + calls = [] + def row_content(row): + calls.append(row) + return row.A + + table = Tabulator(df, embed_content=True, row_content=row_content) + + model = table.get_root(document, comm) + + assert len(calls) == len(df) + + assert len(model.children) == len(df) + + for i, r in df.iterrows(): + assert i in model.children + row = model.children[i] + assert row.text == f"<pre>{r.A}</pre>" + + # Expanding a row should not call row_content again in this context. + table.expanded = [1] + + assert len(calls) == len(df) + + def test_tabulator_selected_and_filtered_dataframe(document, comm): df = makeMixedDataFrame() table = Tabulator(df, selection=list(range(len(df)))) diff --git a/panel/widgets/tables.py b/panel/widgets/tables.py index 754700c0fe..a70e38adfe 100644 --- a/panel/widgets/tables.py +++ b/panel/widgets/tables.py @@ -1484,10 +1484,15 @@ def _indexes_changed(self, old, new): def _update_children(self, *events): cleanup, reuse = set(), set() page_events = ('page', 'page_size', 'pagination') + old_panels = self._child_panels for event in events: if event.name == 'expanded' and len(events) == 1: - cleanup = set(event.old) - set(event.new) - reuse = set(event.old) & set(event.new) + if self.embed_content: + cleanup = set() + reuse = set(old_panels) + else: + cleanup = set(event.old) - set(event.new) + reuse = set(event.old) & set(event.new) elif ( (event.name == 'value' and self._indexes_changed(event.old, event.new)) or (event.name in page_events and not self._updating) or @@ -1495,7 +1500,6 @@ def _update_children(self, *events): ): self.expanded = [] return - old_panels = self._child_panels self._child_panels = child_panels = self._get_children( {i: old_panels[i] for i in reuse} )