Skip to content

Commit

Permalink
skip building new children when embedding (#6881)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored May 30, 2024
1 parent 55f71a9 commit 2a401bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
30 changes: 29 additions & 1 deletion panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))))
Expand Down
10 changes: 7 additions & 3 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,18 +1484,22 @@ 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
(self.pagination == 'remote' and event.name == 'sorters')
):
self.expanded = []
return
old_panels = self._child_panels
self._child_panels = child_panels = self._get_children(
{i: old_panels[i] for i in reuse}
)
Expand Down

0 comments on commit 2a401bb

Please sign in to comment.