散景3.6.0
使用 Bokeh 服务器,我尝试打开一个
bokeh.models.Dialog
模型。通常,这是通过 my_button.js_on_click(OpenDialog(Dialog=dialog))
完成的
现在,我正在努力根据
Dropdown
模型打开给定的对话框。这是一个最小的例子:
from bokeh.models import (
Div,
CustomJS,
Dropdown,
Dialog,
OpenDialog,
CloseDialog,
)
from bokeh.plotting import curdoc
d1 = Dialog(title="Dialog 1", content=Div(text="<h1>Dialog 1</h1>"))
d2 = Dialog(title="Dialog 2", content=Div(text="<h1>Dialog 2</h1>"))
menu = Dropdown(label="Test", menu=["option1", "option2"])
# This works, but always open the same Dialog...
# menu.js_on_click(OpenDialog(dialog=d1))
# This is what I would like...
menu.js_on_click(
{menu.menu[0]: OpenDialog(dialog=d1), menu.menu[1]: OpenDialog(dialog=d2)}
)
curdoc().add_root(menu)
学分转到https://discourse.bokeh.org/u/gmerritt123
OpenDialog()
对象有一个execute()
方法:
from bokeh.layouts import column
from bokeh.models import (
Div,
CustomJS,
Dropdown,
Dialog,
OpenDialog,
CloseDialog,
)
from bokeh.plotting import curdoc
d1 = Dialog(title="Dialog 1", content=Div(text="<h1>Dialog 1</h1>"))
d2 = Dialog(title="Dialog 2", content=Div(text="<h1>Dialog 2</h1>"))
# open randomly one of dialog boxes.
# `dialog` to be overwritten later in JS code
open_dialog = OpenDialog(dialog=d1)
menu = Dropdown(label="Test", menu=["option1", "option2"])
trigger_dialog = CustomJS(
args=dict(d1=d1, d2=d2, d=open_dialog),
code="""
if (this.item == 'option1') {
d.dialog = d1
} else if (this.item == 'option2'){
d.dialog = d2
};
d.execute()
""",
)
menu.js_on_click(trigger_dialog)
curdoc().add_root(menu)