From e8789e312f9452f2a7bcf09a88dffcb4cf351bc4 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Fri, 28 May 2021 13:18:26 +0200 Subject: [PATCH] store update callback; also remove update callback when removing plot --- actor.py | 1 + director.py | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/actor.py b/actor.py index 5339078..1f0e3f5 100644 --- a/actor.py +++ b/actor.py @@ -55,6 +55,7 @@ class Actor: def __init__(self, pvname, cache_size=100): self.src, self.plt, self.cache, self._update = decide_src_plt_cache(pvname) + self.cb = None def update(self): self._update(self.plt, self.cache) diff --git a/director.py b/director.py index 23722c5..85e0a4e 100644 --- a/director.py +++ b/director.py @@ -43,24 +43,35 @@ class Director: print("caught:", type(e), e) return - def add_header_widgets(fig): - btn = Button(label="🗙", button_type="light", width=35) - lbl_width = fig.width - btn.width - lbl = Div(text=fig.name, align="center", style={"font-size": "150%"}, width=lbl_width) - res = column(row(btn, lbl), fig, Spacer(height=35)) - btn.on_click(lambda: self.remove_plot(res)) - return res + self.frame(a) + self.add_plot(a) + + def frame(self, a): + fig = a.plt.fig + btn = Button(label="🗙", button_type="light", width=35) + lbl_width = fig.width - btn.width + lbl = Div(text=fig.name, align="center", style={"font-size": "150%"}, width=lbl_width) + framed_fig = column(row(btn, lbl), fig, Spacer(height=35)) + btn.on_click(lambda: self.remove_plot(a)) + a.plt.fig = framed_fig + + + def add_plot(self, a): + print("Add plot:", a) fig = a.plt.fig - fig = add_header_widgets(fig) # self.layout.children.append(fig) self.layout.children.insert(1, fig) # 1 to skip TextInput - self.doc.add_periodic_callback(a.update, 1000) + assert a.cb is None #TODO + a.cb = self.doc.add_periodic_callback(a.update, 1000) - def remove_plot(self, which): - print("Remove plot:", which) - self.layout.children.remove(which) + def remove_plot(self, a): + print("Remove plot:", a) + fig = a.plt.fig + self.layout.children.remove(fig) + assert a.cb is not None #TODO + self.doc.remove_periodic_callback(a.cb)