Skip to content
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

Adding event hooks for the GUI #953

Merged
merged 1 commit into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Release History
0.4.4 (unreleased)
==================

- Improvement (Experimental): Added simulator hooks
- Improvement: Added audible spike sounds
- Improvement (Experimental): Nodes have access to the GUI keyboard state
- Improvement: support for nengo-bio Connections
Expand Down
5 changes: 5 additions & 0 deletions nengo_gui/components/sim_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ def javascript(self):
def message(self, msg):
if msg == 'pause':
self.paused = True
if 'on_pause' in self.page.locals:
self.page.locals['on_pause'](self.page.sim)
elif msg == 'config':
self.send_config_options = True
elif msg == 'continue':
if self.page.sim is None:
self.page.rebuild = True
else:
if 'on_continue' in self.page.locals:
self.page.locals['on_continue'](self.page.sim)
self.paused = False
elif msg == 'reset':
self.paused = True
Expand Down
1 change: 1 addition & 0 deletions nengo_gui/guibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def create_page(self, filename, reset_cfg=False):

def remove_page(self, page):
self._last_access = time.time()
page.close()
self.pages.remove(page)
if (not self._shutting_down and self.settings.auto_shutdown > 0 and
len(self.pages) <= 0):
Expand Down
14 changes: 13 additions & 1 deletion nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ def build(self):
if self.sim is not None:
if self.settings.backend in Page.singleton_sims:
Page.singleton_sims[self.settings.backend] = self
if 'on_start' in self.locals:
self.locals['on_start'](self.sim)

# remove the temporary components added for visualization
for c in self.components:
Expand All @@ -520,15 +522,25 @@ def runner(self):
self.sim.run_steps(self.sim.max_steps)
else:
self.sim.step()
if 'on_step' in self.locals:
self.locals['on_step'](self.sim)
except Exception as err:
if self.finished:
return
line = nengo_gui.exec_env.determine_line_number()
self.error = dict(trace=traceback.format_exc(), line=line)
self.sim = None
while self.sims_to_close:
self.sims_to_close.pop().close()
s = self.sims_to_close.pop()
if 'on_close' in self.locals:
self.locals['on_close'](s)
s.close()

if self.rebuild:
self.build()
self.sim = None

def close(self):
if self.sim is not None:
if 'on_close' in self.locals:
self.locals['on_close'](self.sim)