Files
kabuki/frame.py

67 lines
2.2 KiB
Python

from bokeh.layouts import column, row
from bokeh.models import Button, Div, Spacer
CROSS = "🗙"
GEAR = ""
class Frame:
def __init__(self, plt):
self.plt = plt
self.set = plt.set # make frame act like contained plot
name = plt.name
inner_fig = plt.fig
n_btns = 2
btn_width = 35
lbl_width = inner_fig.width - n_btns * btn_width
self.btn_close = btn_close = Button(label=CROSS, button_type="default", width=btn_width)
self.btn_cfg = btn_cfg = Button(label=GEAR, button_type="default", width=btn_width)
lbl = Div(text=name, align="center", style={"font-size": "150%"}, width=lbl_width)
header = row(btn_close, btn_cfg, lbl)
self.cfg_dialog = cfg_dialog = column()
cfg_dialog.visible = False
#TODO: the following is just a dummy
types = ("light", "default", "primary", "success", "warning", "danger")
for t in types:
btn = Button(label=t, button_type=t, height=35) #TODO: why does this need height to be set? (otherwise buttons are only properly sized on NEXT click!)
cfg_dialog.children.append(btn)
self.btn_cfg.on_click(self.do_click_cfg)
# wrap fig and cfg such that their container can be hidden during the visibility switch
# wrap with another container to maintain size
# combined gives a relatively smooth (albeit slow) transition between the two states
self.inner = inner = row(inner_fig, cfg_dialog)
size_maintainer = row(inner, min_width=inner_fig.width, min_height=inner_fig.height) #TODO: why min_* ?
self.fig = column(header, size_maintainer, Spacer(height=35))
def on_click_close(self, *args, **kwargs):
self.btn_close.on_click(*args, **kwargs)
def do_click_cfg(self):
toggle_button_type(self.btn_cfg, "default", "warning")
self.inner.visible = False
switch_visibility(self.plt.fig, self.cfg_dialog)
self.inner.visible = True
def toggle_button_type(btn, type1, type2):
btn.button_type = type2 if btn.button_type == type1 else type1
def switch_visibility(a, b):
a.visible, b.visible = b.visible, a.visible