From 503f12e31b9b85ad0175328d66c3fd9c639d5af7 Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Mon, 6 Jun 2016 16:19:47 -0400 Subject: [PATCH 1/4] User can provide custom function for HTML display Also allows Processes to define custom HTML for viewing. --- nengo_gui/components/htmlview.py | 23 +++++++++++++---------- nengo_gui/components/netgraph.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/nengo_gui/components/htmlview.py b/nengo_gui/components/htmlview.py index b7486c79..340ca078 100644 --- a/nengo_gui/components/htmlview.py +++ b/nengo_gui/components/htmlview.py @@ -1,5 +1,7 @@ import collections +import nengo + from nengo_gui.components.component import Component @@ -11,8 +13,8 @@ class HTMLView(Component): def __init__(self, obj): super(HTMLView, self).__init__() self.obj = obj - self.obj_output = obj.output self.data = collections.deque() + self.html_function = getattr(obj.output, '_nengo_html_function_') def attach(self, page, config, uid): super(HTMLView, self).attach(page, config, uid) @@ -20,21 +22,22 @@ def attach(self, page, config, uid): def add_nengo_objects(self, page): with page.model: - self.obj.output = self.gather_data + self.node = nengo.Node(self.gather_data, size_in=self.obj.size_out) + self.conn = nengo.Connection(self.obj, self.node, synapse=None) def remove_nengo_objects(self, page): - self.obj.output = self.obj_output + page.model.connections.remove(self.conn) + page.model.nodes.remove(self.node) - def gather_data(self, t, *x): - value = self.obj_output(t, *x) - data = '%g %s' % (t, self.obj_output._nengo_html_) - self.data.append(data) - return value + def gather_data(self, t, x): + self.data.append((t, x)) def update_client(self, client): while len(self.data) > 0: - item = self.data.popleft() - client.write_text(item) + t, x = self.data.popleft() + html = self.html_function(t, x) + out = '%g %s' % (t, html) + client.write_text(out) def javascript(self): info = dict(uid=id(self), label=self.label) diff --git a/nengo_gui/components/netgraph.py b/nengo_gui/components/netgraph.py index efebcbf2..6eb5034b 100644 --- a/nengo_gui/components/netgraph.py +++ b/nengo_gui/components/netgraph.py @@ -532,7 +532,7 @@ def get_extra_info(self, obj): isinstance(obj.output, OverriddenOutput) and obj.output.base_output is None): info['passthrough'] = True - if callable(obj.output) and hasattr(obj.output, '_nengo_html_'): + if hasattr(obj.output, '_nengo_html_function_'): info['html'] = True info['dimensions'] = int(obj.size_out) elif isinstance(obj, nengo.Ensemble): From 9a5d49fb3315b812efb9d3095f541c728cd8d95b Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Wed, 23 Nov 2016 11:22:51 -0500 Subject: [PATCH 2/4] fixup! User can provide custom function for HTML display --- nengo_gui/components/htmlview.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nengo_gui/components/htmlview.py b/nengo_gui/components/htmlview.py index 340ca078..64b599a1 100644 --- a/nengo_gui/components/htmlview.py +++ b/nengo_gui/components/htmlview.py @@ -30,12 +30,11 @@ def remove_nengo_objects(self, page): page.model.nodes.remove(self.node) def gather_data(self, t, x): - self.data.append((t, x)) + self.data.append((t, self.html_function(t, x))) def update_client(self, client): while len(self.data) > 0: - t, x = self.data.popleft() - html = self.html_function(t, x) + t, html = self.data.popleft() out = '%g %s' % (t, html) client.write_text(out) From 70284b483d36290b897f08682b1b22cfcadd82cb Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Wed, 23 Nov 2016 12:50:38 -0500 Subject: [PATCH 3/4] fixup! Fix for brittle SPA test on Python 2 --- nengo_gui/components/htmlview.py | 39 +++++++++++++++++++++++--------- nengo_gui/components/netgraph.py | 2 +- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/nengo_gui/components/htmlview.py b/nengo_gui/components/htmlview.py index 64b599a1..b82a2922 100644 --- a/nengo_gui/components/htmlview.py +++ b/nengo_gui/components/htmlview.py @@ -14,29 +14,46 @@ def __init__(self, obj): super(HTMLView, self).__init__() self.obj = obj self.data = collections.deque() - self.html_function = getattr(obj.output, '_nengo_html_function_') + + self.static = not callable(obj.output._nengo_html_) + if not self.static: + assert obj.size_out > 0 + self.html = obj.output._nengo_html_ def attach(self, page, config, uid): super(HTMLView, self).attach(page, config, uid) self.label = page.get_label(self.obj) def add_nengo_objects(self, page): - with page.model: - self.node = nengo.Node(self.gather_data, size_in=self.obj.size_out) - self.conn = nengo.Connection(self.obj, self.node, synapse=None) + if self.static: + self.obj_output = self.obj.output + self.obj.output = self.gather_data + else: + with page.model: + self.node = nengo.Node( + self.gather_data, size_in=self.obj.size_out) + self.conn = nengo.Connection(self.obj, self.node, synapse=None) def remove_nengo_objects(self, page): - page.model.connections.remove(self.conn) - page.model.nodes.remove(self.node) + if self.static: + self.obj.output = self.obj_output + else: + page.model.connections.remove(self.conn) + page.model.nodes.remove(self.node) - def gather_data(self, t, x): - self.data.append((t, self.html_function(t, x))) + def gather_data(self, t, *x): + if self.static: + value = self.obj_output(t, *x) + html = self.obj_output._nengo_html_ + else: + value = None + html = self.html(t, *x) + self.data.append('%g %s' % (t, html)) + return value def update_client(self, client): while len(self.data) > 0: - t, html = self.data.popleft() - out = '%g %s' % (t, html) - client.write_text(out) + client.write_text(self.data.popleft()) def javascript(self): info = dict(uid=id(self), label=self.label) diff --git a/nengo_gui/components/netgraph.py b/nengo_gui/components/netgraph.py index 6eb5034b..d486d50a 100644 --- a/nengo_gui/components/netgraph.py +++ b/nengo_gui/components/netgraph.py @@ -532,7 +532,7 @@ def get_extra_info(self, obj): isinstance(obj.output, OverriddenOutput) and obj.output.base_output is None): info['passthrough'] = True - if hasattr(obj.output, '_nengo_html_function_'): + if hasattr(obj.output, '_nengo_html_'): info['html'] = True info['dimensions'] = int(obj.size_out) elif isinstance(obj, nengo.Ensemble): From 04485a582b4d5d437f89e67812c2296f6c4f4f95 Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Wed, 23 Nov 2016 13:00:52 -0500 Subject: [PATCH 4/4] fixup! User can provide custom function for HTML display --- nengo_gui/components/htmlview.py | 34 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/nengo_gui/components/htmlview.py b/nengo_gui/components/htmlview.py index b82a2922..c609e68e 100644 --- a/nengo_gui/components/htmlview.py +++ b/nengo_gui/components/htmlview.py @@ -12,42 +12,44 @@ class HTMLView(Component): def __init__(self, obj): super(HTMLView, self).__init__() - self.obj = obj self.data = collections.deque() - self.static = not callable(obj.output._nengo_html_) - if not self.static: - assert obj.size_out > 0 - self.html = obj.output._nengo_html_ + self.obj = obj + self.obj_output = self.obj.output + self.callable_html = callable(obj.output._nengo_html_) + if self.callable_html: + assert self.obj.size_out > 0 + else: + assert callable(self.obj_output) def attach(self, page, config, uid): super(HTMLView, self).attach(page, config, uid) self.label = page.get_label(self.obj) def add_nengo_objects(self, page): - if self.static: - self.obj_output = self.obj.output - self.obj.output = self.gather_data - else: + if self.callable_html: with page.model: self.node = nengo.Node( self.gather_data, size_in=self.obj.size_out) self.conn = nengo.Connection(self.obj, self.node, synapse=None) + else: + self.obj.output = self.gather_data def remove_nengo_objects(self, page): - if self.static: - self.obj.output = self.obj_output - else: + if self.callable_html: page.model.connections.remove(self.conn) page.model.nodes.remove(self.node) + else: + self.obj.output = self.obj_output def gather_data(self, t, *x): - if self.static: + if self.callable_html: + value = None + html = self.obj_output._nengo_html_(t, *x) + else: value = self.obj_output(t, *x) html = self.obj_output._nengo_html_ - else: - value = None - html = self.html(t, *x) + self.data.append('%g %s' % (t, html)) return value