澄清散景的回调
Clarification on bokeh's callback
bokeh 中的回调函数通常有三个参数:attr
、old
和 new
,如示例中所示documentation
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)
我的问题是:
- 为什么函数体内没有使用
attr
my_text_input_handler
?它实际上是什么 (attr
)?
- 方法中的参数是什么
"value"
text_input.on_change("value"...)
?
- 在 运行 curdoc() 之后,显示了一个带有值(“默认值”)的文本框。然后我将该值(“默认”)更改为“测试”,但我认为会生成 2 句话“Previous label: default”和“Updated label: test”。
谢谢。
来自文档:
All widgets have an .on_change method that takes an attribute name and one or more event handlers as parameters. These handlers are expected to have the function signature, (attr, old, new), where attr refers to the changed attribute’s name, and old and new refer to the previous and updated values of the attribute.
attr
没有在函数体内使用 my_text_input_handler 因为它不依赖数据或对数据做任何事情。所有回调 共享相同的签名 、func(attr, old, new)
,并且此示例不对 attr
执行任何操作。为什么不使用它的简短回答:没有特别的原因...
您可以像这样轻松地重写它:
def my_text_input_handler(attr, old, new):
print("attr: " + attr)
print("Previous label: " + old)
print("Updated label: " + new)
attr
对应text_input.on_change("value", my_text_input_handler)
.
中的“值”
- 传递给
text_input.on_change
函数的“值”参数是您正在观看的 text_input
属性的名称。因此,每次 text_input
对象的“value”属性发生变化时,回调函数 (my_text_input_handler
) 都会被调用,并带有属性名称 (“value”) 以及旧值和新值。
看看这个 HTML/Javascript 示例,以更好地理解它在做什么:https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange2
这是一个示例,可以帮助说明回调的作用。将此代码保存为“demo.py”,通过 bokeh serve demo.py
运行 并查看其工作原理。
'''
Use the ``bokeh serve`` command to run the example by executing:
bokeh serve demo.py
at your command prompt. Then navigate to the URL
http://localhost:5006/demo
in your browser.
'''
from bokeh.io import curdoc
from bokeh.models import TextInput
def my_text_input_handler(attr, old, new):
print("attr: " + attr)
print("Previous label: " + old)
print("Updated label: " + new)
text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)
curdoc().add_root(text_input)
- 检查 terminal/command 行的输出,而不是浏览器的 javascript 控制台。
您的原始代码确实应该打印:
Previous label: default
Updated label: test
bokeh 中的回调函数通常有三个参数:attr
、old
和 new
,如示例中所示documentation
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)
我的问题是:
- 为什么函数体内没有使用
attr
my_text_input_handler
?它实际上是什么 (attr
)? - 方法中的参数是什么
"value"
text_input.on_change("value"...)
? - 在 运行 curdoc() 之后,显示了一个带有值(“默认值”)的文本框。然后我将该值(“默认”)更改为“测试”,但我认为会生成 2 句话“Previous label: default”和“Updated label: test”。
谢谢。
来自文档:
All widgets have an .on_change method that takes an attribute name and one or more event handlers as parameters. These handlers are expected to have the function signature, (attr, old, new), where attr refers to the changed attribute’s name, and old and new refer to the previous and updated values of the attribute.
attr
没有在函数体内使用 my_text_input_handler 因为它不依赖数据或对数据做任何事情。所有回调 共享相同的签名 、func(attr, old, new)
,并且此示例不对attr
执行任何操作。为什么不使用它的简短回答:没有特别的原因...
您可以像这样轻松地重写它:
def my_text_input_handler(attr, old, new):
print("attr: " + attr)
print("Previous label: " + old)
print("Updated label: " + new)
attr
对应text_input.on_change("value", my_text_input_handler)
.
- 传递给
text_input.on_change
函数的“值”参数是您正在观看的text_input
属性的名称。因此,每次text_input
对象的“value”属性发生变化时,回调函数 (my_text_input_handler
) 都会被调用,并带有属性名称 (“value”) 以及旧值和新值。
看看这个 HTML/Javascript 示例,以更好地理解它在做什么:https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange2
这是一个示例,可以帮助说明回调的作用。将此代码保存为“demo.py”,通过 bokeh serve demo.py
运行 并查看其工作原理。
'''
Use the ``bokeh serve`` command to run the example by executing:
bokeh serve demo.py
at your command prompt. Then navigate to the URL
http://localhost:5006/demo
in your browser.
'''
from bokeh.io import curdoc
from bokeh.models import TextInput
def my_text_input_handler(attr, old, new):
print("attr: " + attr)
print("Previous label: " + old)
print("Updated label: " + new)
text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)
curdoc().add_root(text_input)
- 检查 terminal/command 行的输出,而不是浏览器的 javascript 控制台。
您的原始代码确实应该打印:
Previous label: default
Updated label: test