|
3 | 3 | # Copyright (c) IPython Development Team. |
4 | 4 | # Distributed under the terms of the Modified BSD License. |
5 | 5 |
|
| 6 | +import uuid |
6 | 7 | from typing import Optional |
7 | 8 |
|
8 | 9 | import comm.base_comm |
9 | 10 | import traitlets.config |
| 11 | +from traitlets import Bool, Bytes, Instance, Unicode, default |
10 | 12 |
|
11 | 13 | from ipykernel.jsonutil import json_clean |
12 | 14 | from ipykernel.kernelbase import Kernel |
@@ -43,8 +45,30 @@ def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): |
43 | 45 | class Comm(traitlets.config.LoggingConfigurable, BaseComm): |
44 | 46 | """Class for communicating between a Frontend and a Kernel""" |
45 | 47 |
|
| 48 | + kernel = Instance("ipykernel.kernelbase.Kernel", allow_none=True) # type:ignore[assignment] |
| 49 | + comm_id = Unicode() |
| 50 | + primary = Bool(True, help="Am I the primary or secondary Comm?") |
| 51 | + |
| 52 | + target_name = Unicode("comm") |
| 53 | + target_module = Unicode( |
| 54 | + None, |
| 55 | + allow_none=True, |
| 56 | + help="""requirejs module from |
| 57 | + which to load comm target.""", |
| 58 | + ) |
| 59 | + |
| 60 | + topic = Bytes() |
| 61 | + |
| 62 | + @default("kernel") |
| 63 | + def _default_kernel(self): |
| 64 | + if Kernel.initialized(): |
| 65 | + return Kernel.instance() |
| 66 | + |
| 67 | + @default("comm_id") |
| 68 | + def _default_comm_id(self): |
| 69 | + return uuid.uuid4().hex |
| 70 | + |
46 | 71 | def __init__(self, *args, **kwargs): |
47 | | - self.kernel = None |
48 | 72 | # Comm takes positional arguments, LoggingConfigurable does not, so we explicitly forward arguments |
49 | 73 | traitlets.config.LoggingConfigurable.__init__(self, **kwargs) |
50 | 74 | BaseComm.__init__(self, *args, **kwargs) |
|
0 commit comments