10BC0 Defer Jupyter Comm initialization until frontend is ready by philippjfr · Pull Request #6229 · holoviz/panel · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Defer Jupyter Comm initialization until frontend is ready
  • Loading branch information
philippjfr committed Jan 18, 2024
commit 56ce133a712fd69645817168fa9d1ed412370566
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 con 8000 textmanager
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