Files
kabuki/director.py
T

64 lines
1.7 KiB
Python

from random import choice
from bokeh.layouts import column, row
from bokeh.models import Button, Div, Spacer, TextInput
from bokeh.plotting import curdoc
from actor import Actor
class Director:
def __init__(self):
self.doc = doc = curdoc()
doc.title = choice("👺👹") + " kabuki"
ti_add_pv = TextInput(value="", title="Add PV:")
ti_add_pv.on_change("value", self.on_add_pv)
self.layout = layout = column(ti_add_pv)
doc.add_root(layout)
def on_add_pv(self, attr, old, new):
print(f"CB Add PV: attribute \"{attr}\" changed from \"{old}\" to \"{new}\"")
new = new.replace(",", " ").split()
new = reversed(new)
self.add_pvs(*new)
def add_pvs(self, *pvnames):
for n in pvnames:
self.add_pv(n)
def add_pv(self, pvname):
print("Add PV:", pvname)
try:
a = Actor(pvname)
except Exception as e:
print("caught:", type(e), e)
return
def add_header_widgets(fig):
btn = Button(label="🗙", button_type="light", width=35)
lbl = Div(text=fig.name, align="center", style={"font-size": "150%"})
res = column(row(btn, lbl), fig, Spacer(height=35))
btn.on_click(lambda: self.remove_plot(res))
return res
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)
def remove_plot(self, which):
print("Remove plot:", which)
self.layout.children.remove(which)