1 0
import logging
2

3 0
from bokeh.document.events import MessageSentEvent
4 0
from ipykernel.comm import CommManager
5 0
from ipywidgets_bokeh.kernel import BokehKernel, SessionWebsocket, WebsocketStream
6

7

8 0
class PanelSessionWebsocket(SessionWebsocket):
9

10 0
    def __init__(self, *args, **kwargs):
11 0
        self._document = kwargs.pop('document', None)
12 0
        self._queue = []
13 0
        super(PanelSessionWebsocket, self).__init__(*args, **kwargs)
14

15 0
    def send(self, stream, msg_type, content=None, parent=None, ident=None, buffers=None, track=False, header=None, metadata=None):
16 0
        msg = self.msg(msg_type, content=content, parent=parent, header=header, metadata=metadata)
17 0
        msg['channel'] = stream.channel
18

19 0
        doc = self._document
20 0
        doc.on_message("ipywidgets_bokeh", self.receive)
21

22 0
        packed = self.pack(msg)
23

24 0
        if buffers is not None and len(buffers) != 0:
25 0
            buffers = [packed] + buffers
26 0
            nbufs = len(buffers)
27

28 0
            start = 4*(1 + nbufs)
29 0
            offsets = [start]
30

31 0
            for buffer in buffers[:-1]:
32 0
                start += len(buffer)
33 0
                offsets.append(start)
34

35 0
            u32 = lambda n: n.to_bytes(4, "big")
36 0
            items = [u32(nbufs)] + [ u32(offset) for offset in offsets ] + buffers
37 0
            data = b"".join(items)
38
        else:
39 0
            data = packed.decode("utf-8")
40

41 0
        event = MessageSentEvent(doc, "ipywidgets_bokeh", data)
42 0
        self._queue.append(event)
43 0
        doc.add_next_tick_callback(self._dispatch)
44

45 0
    def _dispatch(self):
46 0
        try:
47 0
            for event in self._queue:
48 0
                self._document._trigger_on_change(event)
49 0
        except Exception:
50 0
            pass
51
        finally:
52 0
            self._queue = []
53

54

55 0
class PanelKernel(BokehKernel):
56

57 0
    def __init__(self, key=None, document=None):
58 0
        super(BokehKernel, self).__init__()
59

60 0
        self.session = PanelSessionWebsocket(document=document, parent=self, key=key)
61 0
        self.stream = self.iopub_socket = WebsocketStream(self.session)
62

63 0
        self.iopub_socket.channel = 'iopub'
64 0
        self.session.stream = self.iopub_socket
65 0
        self.comm_manager = CommManager(parent=self, kernel=self)
66 0
        self.shell = None
67 0
        self.log = logging.getLogger('fake')
68

69 0
        comm_msg_types = ['comm_open', 'comm_msg', 'comm_close']
70 0
        for msg_type in comm_msg_types:
71 0
            self.shell_handlers[msg_type] = getattr(self.comm_manager, msg_type)

Read our documentation on viewing source code .

Loading