1
1
from .jsonutil import json_clean
2
2
from nbformat .v4 import output_from_msg
3
+ from typing import Dict , List , Any , Optional
4
+
5
+ from jupyter_client .client import KernelClient
6
+ from nbclient import NotebookClient
3
7
4
8
5
9
class OutputWidget :
6
10
"""This class mimics a front end output widget"""
7
- def __init__ (self , comm_id , state , kernel_client , executor ):
8
- self .comm_id = comm_id
9
- self .state = state
10
- self .kernel_client = kernel_client
11
- self .executor = executor
12
- self .topic = ('comm-%s' % self .comm_id ).encode ('ascii' )
13
- self .outputs = self .state ['outputs' ]
14
- self .clear_before_next_output = False
11
+ def __init__ (
12
+ self ,
13
+ comm_id : str ,
14
+ state : Dict [str , Any ],
15
+ kernel_client : KernelClient ,
16
+ executor : NotebookClient ) -> None :
17
+
18
+ self .comm_id : str = comm_id
19
+ self .state : Dict [str , Any ] = state
20
+ self .kernel_client : KernelClient = kernel_client
21
+ self .executor : NotebookClient = executor
22
+ self .topic : bytes = ('comm-%s' % self .comm_id ).encode ('ascii' )
23
+ self .outputs : List = self .state ['outputs' ]
24
+ self .clear_before_next_output : bool = False
25
+
26
+ def clear_output (
27
+ self ,
28
+ outs : List ,
29
+ msg : Dict ,
30
+ cell_index : int ) -> None :
15
31
16
- def clear_output (self , outs , msg , cell_index ):
17
32
self .parent_header = msg ['parent_header' ]
18
33
content = msg ['content' ]
19
34
if content .get ('wait' ):
@@ -26,12 +41,18 @@ def clear_output(self, outs, msg, cell_index):
26
41
# sync the state to the nbconvert state as well, since that is used for testing
27
42
self .executor .widget_state [self .comm_id ]['outputs' ] = self .outputs
28
43
29
- def sync_state (self ):
44
+ def sync_state (self ) -> None :
30
45
state = {'outputs' : self .outputs }
31
46
msg = {'method' : 'update' , 'state' : state , 'buffer_paths' : []}
32
47
self .send (msg )
33
48
34
- def _publish_msg (self , msg_type , data = None , metadata = None , buffers = None , ** keys ):
49
+ def _publish_msg (
50
+ self ,
51
+ msg_type : str ,
52
+ data : Optional [Dict ] = None ,
53
+ metadata : Optional [Dict ] = None ,
54
+ buffers : Optional [List ] = None ,
55
+ ** keys ) -> None :
35
56
"""Helper for sending a comm message on IOPub"""
36
57
data = {} if data is None else data
37
58
metadata = {} if metadata is None else metadata
@@ -40,10 +61,21 @@ def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys)
40
61
metadata = metadata )
41
62
self .kernel_client .shell_channel .send (msg )
42
63
43
- def send (self , data = None , metadata = None , buffers = None ):
64
+ def send (
65
+ self ,
66
+ data : Optional [Dict ] = None ,
67
+ metadata : Optional [Dict ] = None ,
68
+ buffers : Optional [List ] = None ) -> None :
69
+
44
70
self ._publish_msg ('comm_msg' , data = data , metadata = metadata , buffers = buffers )
45
71
46
- def output (self , outs , msg , display_id , cell_index ):
72
+ def output (
73
+ self ,
74
+ outs : List ,
75
+ msg : Dict ,
76
+ display_id : str ,
77
+ cell_index : int ) -> None :
78
+
47
79
if self .clear_before_next_output :
48
80
self .outputs = []
49
81
self .clear_before_next_output = False
@@ -66,7 +98,7 @@ def output(self, outs, msg, display_id, cell_index):
66
98
# sync the state to the nbconvert state as well, since that is used for testing
67
99
self .executor .widget_state [self .comm_id ]['outputs' ] = self .outputs
68
100
69
- def set_state (self , state ) :
101
+ def set_state (self , state : Dict ) -> None :
70
102
if 'msg_id' in state :
71
103
msg_id = state .get ('msg_id' )
72
104
if msg_id :
@@ -76,7 +108,7 @@ def set_state(self, state):
76
108
self .executor .remove_output_hook (self .msg_id , self )
77
109
self .msg_id = msg_id
78
110
79
- def handle_msg (self , msg ) :
111
+ def handle_msg (self , msg : Dict ) -> None :
80
112
content = msg ['content' ]
81
113
comm_id = content ['comm_id' ]
82
114
assert comm_id == self .comm_id
0 commit comments