Skip to content

Commit c5bfcd8

Browse files
committed
Show errors on output boxes
1 parent 35350ee commit c5bfcd8

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

ipynao/nao_robot.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
TODO: Add module docstring
99
"""
1010

11-
from ipywidgets import DOMWidget
11+
from ipywidgets import DOMWidget, Output
1212
from traitlets import Unicode, Integer
1313
from ._frontend import module_name, module_version
1414
import asyncio
@@ -20,9 +20,10 @@ class NaoRobotService():
2020
widget = None
2121
output = None
2222

23-
def __init__(self, widget, service_name):
23+
def __init__(self, widget, service_name, output=Output()):
2424
self.name = service_name
2525
self.widget = widget
26+
self.output = output
2627

2728
def _create_msg(self, method_name, *args, **kwargs):
2829
data = {}
@@ -43,7 +44,9 @@ async def async_call_service(self, method_name, *args, **kwargs):
4344
self.widget.send(data)
4445

4546
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)
4750
except Exception as e:
4851
return e
4952
return self.widget.response
@@ -70,6 +73,7 @@ class NaoRobotWidget(DOMWidget):
7073
status = Unicode("Not busy").tag(sync=True)
7174
counter = Integer(0).tag(sync=True)
7275
response = None
76+
js_error = None
7377

7478

7579
def __init__(self, **kwargs):
@@ -79,17 +83,26 @@ def __init__(self, **kwargs):
7983

8084
def _handle_frontend_msg(self, model, msg, buffer):
8185
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"]
8390

8491

85-
def wait_for_change(widget, value_name):
92+
def wait_for_change(widget, value_name, output=Output()):
8693
future = asyncio.Future()
8794
widget.response = None
95+
widget.js_error = None
8896

8997
def get_value_change(change):
9098
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)
93106
else:
94107
future.set_result(change)
95108

@@ -105,9 +118,9 @@ def connect(self, ip_address="nao.local", port="80"):
105118
self.send(data)
106119

107120

108-
def service(self, service_name):
121+
def service(self, service_name, output=Output()):
109122
data = {}
110123
data["command"] = str("createService")
111124
data["service"] = str(service_name)
112125
self.send(data)
113-
return NaoRobotService(self, service_name)
126+
return NaoRobotService(self, service_name, output)

src/widget.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ export class NaoRobotModel extends DOMWidgetModel {
140140
// Wait for service to become available
141141
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
142142

143-
// Timeout after ~5 seconds
144-
for (let i = 0; i < 50; i++) {
143+
// Timeout after ~10 seconds
144+
for (let i = 0; i < 100; i++) {
145145
if (this._services[serviceName] !== undefined) {
146146
console.log('Service available after ', i / 10.0, ' seconds.');
147147
break;
@@ -160,13 +160,18 @@ export class NaoRobotModel extends DOMWidgetModel {
160160
await servicePromise
161161
.then((resolution: any) => {
162162
this.changeStatus('Task completed');
163-
if (resolution !== undefined) {
164-
this.send(resolution);
165-
}
163+
this.send({
164+
isError: false,
165+
data: resolution ?? true
166+
});
167+
166168
})
167169
.catch((rejection: string) => {
168170
this.changeStatus(rejection);
169-
this.send(rejection);
171+
this.send({
172+
isError: true,
173+
data: rejection
174+
});
170175
});
171176

172177
this.set('counter', this.get('counter') + 1);

0 commit comments

Comments
 (0)