|
8 | 8 | import os |
9 | 9 | import sys |
10 | 10 | import uuid |
| 11 | +import warnings |
11 | 12 |
|
12 | 13 | from collections import OrderedDict |
13 | 14 | from contextlib import contextmanager |
| 15 | +from functools import partial |
14 | 16 | from typing import ( |
15 | 17 | TYPE_CHECKING, Any, Dict, Iterator, List, Literal, Optional, Tuple, |
16 | 18 | ) |
|
63 | 65 | def _jupyter_server_extension_paths() -> List[Dict[str, str]]: |
64 | 66 | return [{"module": "panel.io.jupyter_server_extension"}] |
65 | 67 |
|
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: |
67 | 69 | """ |
68 | 70 | Pushes events stored on the document across the provided comm. |
69 | 71 | """ |
70 | | - msg = diff(doc, binary=binary) |
| 72 | + if msg is None: |
| 73 | + msg = diff(doc, binary=binary) |
71 | 74 | if msg is None: |
72 | 75 | 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 | + """ |
73 | 93 | # WARNING: CommManager model assumes that either JSON content OR a buffer |
74 | 94 | # is sent. Therefore we must NEVER(!!!) send both at once. |
75 | 95 | comm.send(msg.header_json) |
|
0 commit comments