我想弄清楚如何使用 Bokeh 显示用户的输入。示例代码如下。任何指针将不胜感激。
谢谢
from bokeh.layouts import widgetbox
from bokeh.models import CustomJS, TextInput, Paragraph
from bokeh.plotting import output_file, show
# SAVE
output_file('Sample_Application.html',mode='inline',root_dir=None)
# PREP DATA
welcome_message = 'You have selected: (none)'
# CALLBACKS
def callback_print(source=None, window=None):
user_input = str(cb_obj.value)
welcome_message = 'You have selected: ' + user_input
source.trigger('change')
# TAKE ONLY OUTPUT
text_banner = Paragraph(text=welcome_message, width=200, height=100)
# USER INTERACTIONS
text_input = TextInput(value="", title="Enter row number:",
callback=CustomJS.from_py_func(callback_print))
# LAYOUT
widg = widgetbox(text_input, text_banner)
show(widg)
Run Code Online (Sandbox Code Playgroud)
很少有问题,您需要实际将文本横幅对象传入python 回调,并将文本属性更新为新字符串。
当前,您正在传入未定义的“源”并试图触发更改。通常,当您更改源数据并更新它以显示在表格或绘图等上时,您会执行此操作...
包含在必要的修复下面
from bokeh.layouts import widgetbox
from bokeh.models import CustomJS, TextInput, Paragraph
from bokeh.plotting import output_file, show
# SAVE
output_file('Sample_Application.html',mode='inline',root_dir=None)
# PREP DATA
welcome_message = 'You have selected: (none)'
# TAKE ONLY OUTPUT
text_banner = Paragraph(text=welcome_message, width=200, height=100)
# CALLBACKS
def callback_print(text_banner=text_banner):
user_input = str(cb_obj.value)
welcome_message = 'You have selected: ' + user_input
text_banner.text = welcome_message
# USER INTERACTIONS
text_input = TextInput(value="", title="Enter row number:",
callback=CustomJS.from_py_func(callback_print))
# LAYOUT
widg = widgetbox(text_input, text_banner)
show(widg)
Run Code Online (Sandbox Code Playgroud)