-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide function for HTML display #755
base: master
Are you sure you want to change the base?
Changes from all commits
503f12e
9a5d49f
70284b4
04485a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import collections | ||
|
||
import nengo | ||
|
||
from nengo_gui.components.component import Component | ||
|
||
|
||
|
@@ -10,31 +12,50 @@ class HTMLView(Component): | |
|
||
def __init__(self, obj): | ||
super(HTMLView, self).__init__() | ||
self.obj = obj | ||
self.obj_output = obj.output | ||
self.data = collections.deque() | ||
|
||
self.obj = obj | ||
self.obj_output = self.obj.output | ||
self.callable_html = callable(obj.output._nengo_html_) | ||
if self.callable_html: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that this attribute is checked in almost every method makes me wonder if this might be a case for polymorphism. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Maybe provide a helper function that automatically instantiates the right class. Something like def HTMLView(obj):
if callable(obj.output._nengo_htlm):
return CallableHTMLView(obj)
else
return StaticHTMLView(obj) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this out. In some ways it's cleaner, but the inheritance does add it's own form of cognitive complexity. Frankly, I'm just not sure if this class is large enough to really benefit from inheritance. And now, I'm getting an error: I've put the code in the |
||
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): | ||
with page.model: | ||
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): | ||
self.obj.output = self.obj_output | ||
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): | ||
value = self.obj_output(t, *x) | ||
data = '%g %s' % (t, self.obj_output._nengo_html_) | ||
self.data.append(data) | ||
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_ | ||
|
||
self.data.append('%g %s' % (t, html)) | ||
return value | ||
|
||
def update_client(self, client): | ||
while len(self.data) > 0: | ||
item = self.data.popleft() | ||
client.write_text(item) | ||
client.write_text(self.data.popleft()) | ||
|
||
def javascript(self): | ||
info = dict(uid=id(self), label=self.label) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a difference between
self.obj_output
andself.obj.output
? There's one lineself.obj.output = self.obj_output
in a function which is doing the opposite assignment and I don't understand why. The two variables don't seem to be kept in sync and it seems to be random which one is used where.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
self.obj_output
always represents the original node function. Ifnot callable_html
, then we need to keep this around since we overwrite the node output, and need to be able to set it back inremove_nengo_objects
. It's not random which is used where:self.obj.output
is only used when we're setting or re-setting the node output, andself.obj_output
is used whenever we want to call the old node function. This is consistent with how this class currently works inmaster
.