我试图弄清楚如何用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)
答案 0 :(得分:3)
很少有问题,您需要将文本标题对象实际传入python回调,并将text属性更新为新字符串。
目前您传入的是“source”,这是未定义的,并尝试触发更改。通常,当您更改源的数据并将其更新为显示在表格或绘图等上时,您会执行此操作...
包含在必要的修复
之下visual_recognition = VisualRecognitionV3('2016-05-20', api_key=param)
答案 1 :(得分:0)
@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