散景:显示用户输入的文本

Bokeh: Display user input text

我正在尝试弄清楚如何使用 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)

几个问题,您需要将文本横幅对象实际传递到 python 回调中,并将文本属性更新为新字符串。

目前您正在传递 "source",这是未定义的并试图触发更改。通常,当您更改源数据并更新它以显示在 table 或绘图等上时,您会这样做...

包含在必要的修复下方

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)

@Anthonydouc 的回答很有帮助,但由于调用了 CustomJS,所以在我的 bokeh 1.4.0 安装中不起作用。

此处发布了适用于 1.4.0 和可能适用于 bokeh 2.0 的类似解决方案:

from bokeh.models import TextInput, Paragraph
from bokeh.plotting import curdoc
from bokeh.layouts import layout

myMessage = 'You have entered nothing yet: (none)'
text_output = Paragraph(text=myMessage, width=200, height=100)

def my_text_input_handler(attr, old, new):
    myMessage="you just entered: {0}".format(new)
    text_output.text=myMessage # this changes the browser display

text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)

layout = column(text_input,text_output)

curdoc().add_root(layout)
curdoc().title = "Bokeh text input example with text echo"

让 .on_change() 事件起作用的唯一方法是使用 bokeh serve,所以 运行 像这样:

bokeh serve --show text_input_example_with_display.py