adding widgets link
,并编写了数据帧生成器。但是我很难把它绑在按钮上。为了确保正确处理事件,我添加了另一个TextInput,以在单击按钮时显示mean TextInput的值。代码如下:
from bokeh.models.widgets import TextInput, Button, Div
from bokeh.layouts import layout, column, row
from bokeh.io import curdoc ## to assign callback to widget
from bokeh import events ## for event handling
from bokeh.models import CustomJS
import numpy as np
import pandas as pd
text_input_mean = TextInput(value="0.0", title="Enter mean:")
text_input_vaiance = TextInput(value="0.0", title="Enter variance:")
text_input_rows = TextInput(value="5", title="Enter num rows:")
text_input_columns = TextInput(value="5", title="Enter num columns:")
button = Button(label = "Generate Dataframe", button_type = "success")
text_output = TextInput(title = 'Python result is shown here: ')
div = Div(text="""Making a form with bokeh mainly to handle events.""",
width=500, height=50)
layout = column(div, row(text_input_mean, text_input_vaiance), row(text_input_rows, text_input_columns),
button, text_output)
show(layout)
## Events with no attributes
# button.js_on_event(events.ButtonClick, button_click_handler) # Button click
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
text_input_mean.on_change("value", my_text_input_handler)
def button_click_handler():
text_output.__setattr__('value', str(text_input_mean.value))
#text_output.value = str(text_input_mean.value)
button.on_click(button_click_handler)
# button.on_click(generate_normal_df)
# curdoc().add_root(layout)
def generate_normal_df(mean, var, x, y):
mean = text_input_mean.value
variance = text_input_vaiance.value
row_num = x
col_num = y
return pd.DataFrame(np.random.normal(loc = mean, scale = variance, size=(row_num, col_num)))
表格如下:
单击按钮时,不会发生任何事情!我也查看了控制台,但也并没有任何内容。我在同一个链接中使用了“my_text_input_handler”方法来查看我是否更改了text_input_的值意味着发生了什么,但也没有更改。我尝试了另一种方法,通过遵循这个
link
button.on_click(generate_normal_df)
curdoc().add_root(layout)
我后来对此发表了评论。但是当我使用这两行代码时,我得到以下错误:
此外,该行:
button.on_click(button_click_handler)
正在我的IDE中导致另一个错误。当我使用按钮单击处理程序()时,会出现以下错误:
文件“C:\Users\E17538\anaconda3\lib\site packages\bokeh\model.py”,第609行,在附件文档中
运行时错误:模型只能由单个文档拥有,WidgetBox(id='1009',…)已在文档中
当我在没有()的情况下使用它时,我再次得到相同的错误。如果您能指导我如何将处理程序绑定到我的小部件,我将不胜感激。非常感谢你。