Skip to content

Commit 7b37a0d

Browse files
committed
Defer Jupyter Comm initialization until frontend is ready (#6229)
1 parent dcb0532 commit 7b37a0d

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

panel/io/notebook.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import os
99
import sys
1010
import uuid
11+
import warnings
1112

1213
from collections import OrderedDict
1314
from contextlib import contextmanager
15+
from functools import partial
1416
from typing import (
1517
TYPE_CHECKING, Any, Dict, Iterator, List, Literal, Optional, Tuple,
1618
)
@@ -63,13 +65,31 @@
6365
def _jupyter_server_extension_paths() -> List[Dict[str, str]]:
6466
return [{"module": "panel.io.jupyter_server_extension"}]
6567

66-
def push(doc: 'Document', comm: 'Comm', binary: bool = True) -> None:
68+
def push(doc: Document, comm: Comm, binary: bool = True, msg: any = None) -> None:
6769
"""
6870
Pushes events stored on the document across the provided comm.
6971
"""
70-
msg = diff(doc, binary=binary)
72+
if msg is None:
73+
msg = diff(doc, binary=binary)
7174
if msg is None:
7275
return
76+
elif not comm._comm:
77+
try:
78+
from tornado.ioloop import IOLoop
79+
IOLoop.current().call_later(0.1, partial(push, doc, comm, binary, msg=msg))
80+
except Exception:
81+
warnings.warn(
82+
'Attempted to send message over Jupyter Comm but it was not '
83+
'yet open and also could not be rescheduled to a later time. '
84+
'The update will not be sent.', UserWarning, stacklevel=0
85+
)
86+
else:
87+
send(comm, msg)
88+
89+
def send(comm: Comm, msg: any):
90+
"""
91+
Sends a bokeh message across a pyviz_comms.Comm.
92+
"""
7393
# WARNING: CommManager model assumes that either JSON content OR a buffer
7494
# is sent. Therefore we must NEVER(!!!) send both at once.
7595
comm.send(msg.header_json)

panel/template/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ def _repr_mimebundle_(
301301
client_comm = state._comm_manager.get_client_comm(
302302
on_msg=partial(self._on_msg, ref, manager),
303303
on_error=partial(self._on_error, ref),
304-
on_stdout=partial(self._on_stdout, ref)
304+
on_stdout=partial(self._on_stdout, ref),
305+
on_open=lambda _: comm.init()
305306
)
306307
manager.client_comm_id = client_comm.id
307308
doc.add_root(manager)

panel/viewable.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ def _render_mimebundle(self, model: Model, doc: Document, comm: Comm, location:
511511
client_comm = state._comm_manager.get_client_comm(
512512
on_msg=functools.partial(self._on_msg, ref, manager),
513513
on_error=functools.partial(self._on_error, ref),
514-
on_stdout=functools.partial(self._on_stdout, ref)
514+
on_stdout=functools.partial(self._on_stdout, ref),
515+
on_open=lambda _: comm.init()
515516
)
516517
self._comms[ref] = (comm, client_comm)
517518
manager.client_comm_id = client_comm.id

0 commit comments

Comments
 (0)