Skip to content

Commit

Permalink
Added on_[step,pause,continue,start,close] hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tcstewar committed Jun 9, 2019
1 parent 19efaaa commit 3fe2772
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
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)

0 comments on commit 3fe2772

Please sign in to comment.