Skip to content

Commit c47d645

Browse files
authored
Only add/remove model from Document once (#6342)
1 parent 0b18620 commit c47d645

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

panel/io/model.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from contextlib import contextmanager
99
from typing import (
10-
TYPE_CHECKING, Any, Iterable, List, Optional,
10+
TYPE_CHECKING, Any, Iterable, List, Optional, Set,
1111
)
1212

1313
import numpy as np
@@ -92,27 +92,33 @@ def diff(
9292
msg.add_buffer(buffer)
9393
return msg
9494

95-
def remove_root(obj: Model, replace: Document | None = None) -> None:
95+
def remove_root(obj: Model, replace: Document | None = None, skip: Set[Model] | None = None) -> None:
9696
"""
9797
Removes the document from any previously displayed bokeh object
9898
"""
99+
models = set()
99100
for model in obj.select({'type': Model}):
101+
if skip and model in skip:
102+
continue
100103
prev_doc = model.document
101104
model._document = None
102105
if prev_doc:
103106
prev_doc.remove_root(model)
104107
if replace:
105108
model._document = replace
109+
models.add(model)
110+
return models
106111

107-
def add_to_doc(obj: Model, doc: Document, hold: bool = False):
112+
def add_to_doc(obj: Model, doc: Document, hold: bool = False, skip: Set[Model] | None = None):
108113
"""
109114
Adds a model to the supplied Document removing it from any existing Documents.
110115
"""
111116
# Add new root
112-
remove_root(obj)
117+
models = remove_root(obj, skip=skip)
113118
doc.add_root(obj)
114119
if doc.callbacks.hold_value is None and hold:
115120
doc.hold()
121+
return models
116122

117123
def patch_cds_msg(model, msg):
118124
"""

panel/template/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def _init_doc(
211211
# Add all render items to the document
212212
objs, models = [], []
213213
sizing_modes = {}
214+
tracked_models = set()
214215
for name, (obj, tags) in self._render_items.items():
215216

216217
# Render root without pre-processing
@@ -241,7 +242,7 @@ def _init_doc(
241242
objs.append(obj)
242243
models.append(model)
243244

244-
add_to_doc(model, document, hold=bool(comm))
245+
tracked_models |= add_to_doc(model, document, hold=bool(comm), skip=tracked_models)
245246
document.on_session_destroyed(obj._server_destroy) # type: ignore
246247

247248
# Here we ensure that the preprocessor is run across all roots

0 commit comments

Comments
 (0)