Bokeh: Display user input text

走远了吗. 提交于 2019-12-11 10:28:00

问题


I am trying to figure out how to display a user's input with Bokeh. Sample code is below. Any pointers would be greatly appreciated.

Thank you

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)

回答1:


Few issues, you need to actually pass in the text banner object into the python callback,and update the text attribute to the new string.

Currently you are passing in "source", which is undefined and trying to trigger a change. Normally you do this when you change the data of a source and update it to display on a table or plot etc...

Included below the necessary fix

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)


来源:https://stackoverflow.com/questions/44101905/bokeh-display-user-input-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!