Allow HTMLView to communicate back to server #727
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The HTMLView component is very useful for making all sorts of arbitrary display things. It'd be kinda nice if it could also be used to make arbitrary input systems. That is, to be able to catch mouse events (or whatever else) that happen in the HTMLView component and pass those back to the server.
I implemented a quick prototype that only required adding 4 lines to nengo_gui. All it does is allow the javascript to send messages back to the python code. For example, here's an HTMLView Node that lets you click on it, and it'll use that as a 2D value that is the output of the Node:
The first addition is adding the WebSocket
ws
to the parentNode of the HTMLView div, which allows thethis.parentNode.ws.send()
call to send arbitrary information back to the server. The second addition is catching that message on the server side and assigning it to the_nengo_message_
variable, so thatpos = getattr(input_function, '_nengo_message_', None)
will work.So, overall, I was happy that this basic functionality can happen with such a small change. I'd like to keep this functionality fairly bare-bones, leaving it up to the individual to implement more complex things (for example, in the above example, I might want to hook into the
onmouseevent
instead ofonmousedown
, or I might want to do a completely different transform on the javascript side). But it might be nice to have some common transformations made a bit easier with helper functions.In any case, I'm mostly going to leave this PR here as it is until we get a few more examples of people using it, and see what sort of polishing might be appropriate.