8
8
TODO: Add module docstring
9
9
"""
10
10
11
- from ipywidgets import DOMWidget
11
+ from ipywidgets import DOMWidget , Output
12
12
from traitlets import Unicode , Integer
13
13
from ._frontend import module_name , module_version
14
14
import asyncio
@@ -20,9 +20,10 @@ class NaoRobotService():
20
20
widget = None
21
21
output = None
22
22
23
- def __init__ (self , widget , service_name ):
23
+ def __init__ (self , widget , service_name , output = Output () ):
24
24
self .name = service_name
25
25
self .widget = widget
26
+ self .output = output
26
27
27
28
def _create_msg (self , method_name , * args , ** kwargs ):
28
29
data = {}
@@ -43,7 +44,9 @@ async def async_call_service(self, method_name, *args, **kwargs):
43
44
self .widget .send (data )
44
45
45
46
try :
46
- await self .widget .wait_for_change ('counter' )
47
+ self .output .clear_output ()
48
+ self .output .append_stdout ("Calling service... \n " )
49
+ await self .widget .wait_for_change ('counter' , self .output )
47
50
except Exception as e :
48
51
return e
49
52
return self .widget .response
@@ -70,6 +73,7 @@ class NaoRobotWidget(DOMWidget):
70
73
status = Unicode ("Not busy" ).tag (sync = True )
71
74
counter = Integer (0 ).tag (sync = True )
72
75
response = None
76
+ js_error = None
73
77
74
78
75
79
def __init__ (self , ** kwargs ):
@@ -79,17 +83,26 @@ def __init__(self, **kwargs):
79
83
80
84
def _handle_frontend_msg (self , model , msg , buffer ):
81
85
print ("Received frontend msg: " , msg )
82
- self .response = msg
86
+ if (msg ["isError" ]):
87
+ self .js_error = msg ["data" ]
88
+ else :
89
+ self .response = msg ["data" ]
83
90
84
91
85
- def wait_for_change (widget , value_name ):
92
+ def wait_for_change (widget , value_name , output = Output () ):
86
93
future = asyncio .Future ()
87
94
widget .response = None
95
+ widget .js_error = None
88
96
89
97
def get_value_change (change ):
90
98
widget .unobserve (get_value_change , names = value_name )
91
- if (widget .response != None ):
92
- future .set_result (widget .response )
99
+ if (widget .response != None or widget .js_error != None ):
100
+ if (widget .js_error ):
101
+ future .set_exception (Exception (widget .js_error ))
102
+ output .append_stderr (widget .js_error )
103
+ else :
104
+ future .set_result (widget .response )
105
+ output .append_stdout (widget .response )
93
106
else :
94
107
future .set_result (change )
95
108
@@ -105,9 +118,9 @@ def connect(self, ip_address="nao.local", port="80"):
105
118
self .send (data )
106
119
107
120
108
- def service (self , service_name ):
121
+ def service (self , service_name , output = Output () ):
109
122
data = {}
110
123
data ["command" ] = str ("createService" )
111
124
data ["service" ] = str (service_name )
112
125
self .send (data )
113
- return NaoRobotService (self , service_name )
126
+ return NaoRobotService (self , service_name , output )
0 commit comments