From 417ec75e7ec3724e082308a8cb745b8130079e8c Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Mon, 6 Jun 2016 16:19:47 -0400 Subject: [PATCH] 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 29abbf38..a2061b51 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(item) + t, x = self.data.popleft() + html = self.html_function(t, x) + out = '%g %s' % (t, html) + client.write(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 a5f727e1..c90ca155 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):