store update callback; also remove update callback when removing plot

This commit is contained in:
2021-05-28 13:18:26 +02:00
parent f4e0a899a2
commit e8789e312f
2 changed files with 24 additions and 12 deletions

View File

@@ -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)

View File

@@ -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)