Skip to content

Commit

Permalink
Defer Jupyter Comm initialization until frontend is ready (#6229)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 18, 2024
1 parent dcb0532 commit 7b37a0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
24 changes: 22 additions & 2 deletions panel/io/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import os
import sys
import uuid
import warnings

from collections import OrderedDict
from contextlib import contextmanager
from functools import partial
from typing import (
TYPE_CHECKING, Any, Dict, Iterator, List, Literal, Optional, Tuple,
)
Expand Down Expand Up @@ -63,13 +65,31 @@
def _jupyter_server_extension_paths() -> List[Dict[str, str]]:
return [{"module": "panel.io.jupyter_server_extension"}]

def push(doc: 'Document', comm: 'Comm', binary: bool = True) -> None:
def push(doc: Document, comm: Comm, binary: bool = True, msg: any = None) -> None:
"""
Pushes events stored on the document across the provided comm.
"""
msg = diff(doc, binary=binary)
if msg is None:
msg = diff(doc, binary=binary)
if msg is None:
return
elif not comm._comm:
try:
from tornado.ioloop import IOLoop
IOLoop.current().call_later(0.1, partial(push, doc, comm, binary, msg=msg))
except Exception:
warnings.warn(
'Attempted to send message over Jupyter Comm but it was not '
'yet open and also could not be rescheduled to a later time. '
'The update will not be sent.', UserWarning, stacklevel=0
)
else:
send(comm, msg)

def send(comm: Comm, msg: any):
"""
Sends a bokeh message across a pyviz_comms.Comm.
"""
# WARNING: CommManager model assumes that either JSON content OR a buffer
# is sent. Therefore we must NEVER(!!!) send both at once.
comm.send(msg.header_json)
Expand Down
3 changes: 2 additions & 1 deletion panel/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def _repr_mimebundle_(
client_comm = state._comm_manager.get_client_comm(
on_msg=partial(self._on_msg, ref, manager),
on_error=partial(self._on_error, ref),
on_stdout=partial(self._on_stdout, ref)
on_stdout=partial(self._on_stdout, ref),
on_open=lambda _: comm.init()
)
manager.client_comm_id = client_comm.id
doc.add_root(manager)
Expand Down
3 changes: 2 additions & 1 deletion panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ def _render_mimebundle(self, model: Model, doc: Document, comm: Comm, location:
client_comm = state._comm_manager.get_client_comm(
on_msg=functools.partial(self._on_msg, ref, manager),
on_error=functools.partial(self._on_error, ref),
on_stdout=functools.partial(self._on_stdout, ref)
on_stdout=functools.partial(self._on_stdout, ref),
on_open=lambda _: comm.init()
)
self._comms[ref] = (comm, client_comm)
manager.client_comm_id = client_comm.id
Expand Down

0 comments on commit 7b37a0d

Please sign in to comment.