python - Bokeh:显示用户输入的文本

标签 python bokeh

我正在尝试弄清楚如何使用 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”并试图触发更改。通常,当您更改源数据并将其更新为显示在表格或绘图等上时,您会执行此操作...

包含在必要的修复下面

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)

关于python - Bokeh:显示用户输入的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101905/

相关文章:

Python 与所有主要专业语言

python - 用于图像处理的 Numpy 操作

jupyter-notebook - jupyter notebook 中没有显示 Bokeh

python - 数据帧 : Drop Down menu to select columns to Display (Bokeh) 的交互图

python - Bokeh :对多个图形使用相同的工具效果

python - 我可以将表从 SQL Server (=MS SQL) 导入到 Python/Pandas 数据框中吗?

python - psycopg2.ProgrammingError : syntax error at or near "%"

python - PySide2 复合小部件悬停效果

python - 使用每个烛台单独着色的自定义烛台图表绘制

python - 使用 Tornado 显示 Bokeh 生成的文件