“on_click_load”仍然是一个本地 python 函数,因此您可以在脚本中简单地访问它。只需用假人(可能是小部件和事件)填写您不需要的变量,并根据您的需要填写数据变量。
如果您需要来自客户端的一些输入,那就更难了。我不知道如何远程控制客户端。到目前为止,我唯一要做的就是使用私有类扩展 VuetifyTemplate 并指定一些 JS 代码在“挂载”时运行。这将运行显示的代码,但与触发点击行为不同:
这是一个简单的示例,它直接将变量的内容复制到本地剪贴板,而不使用任何显示元素:
import ipyvuetify as v
from traitlets import Unicode, observe
class toClipboard(v.VuetifyTemplate):
"""Copies a given string directly to the users clipboard.
Parameters:
clipboardValue - The value to offer for copying to the clipboard
Example:
tunnel = toClipboard(clipboardValue='VALUE_TO_COPY')
Upon change of the variable 'clipboardValue', it's content will be automatically pushed to the clipboard
"""
clipboardValue = Unicode('').tag(sync=True)
template = Unicode('''
<script>
export default {
mounted () {
var tmpElement = $('<textarea>').val(this.clipboardValue).appendTo('body').select();
document.execCommand('copy');
$(tmpElement).remove();
},
}
</script>''').tag(sync=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.observe(self.pushToClipboard, names=('clipboardValue'))
display(self)
def pushToClipboard(self, change):
display(self)
作为一个额外的好处,这个例子使用了 traitlets 的观察函数来在变量的值改变时重新显示 JS。这是一种廉价的解决方法,可以创建一些类似的行为。
我不是在真正的 GUI 中使用上面的示例,而是在 Jupyther Notebook 中作为一种懒惰的方式来自动将计算结果复制到我的本地剪贴板。