[英]trigger javascript from onclick in Dash
我有一个 Python Dash 应用程序正在运行,有几个回调和一个漂亮的图表。 我接下来需要的是有一个按钮,用于将一些结果以 excel 格式(非常具体,是的)复制到剪贴板。 I guess I need to implement a javascript function triggered by a button onclick , but I have no idea how to get the Python app to call the javascript. 有人可以帮我解决这个问题吗? :)
像这样的东西:
function copy_to_cliboard() {
var copyText = document.getElementById("text_input");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
多谢!
如果有人遇到同样的挑战,下面是我的解决方案。
app.clientside_callback(
"""
function placeholder(n_clicks, data) {
window.data_to_copy = data.data;
var copyText = document.getElementById("text_input");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
}
// Overwrite what is being copied to the clipboard.
document.addEventListener('copy', function(e){
// e.clipboardData is initially empty, but we can set it to the
// data that we want copied onto the clipboard.
e.clipboardData.setData('text/plain', window.data_to_copy);
// This is necessary to prevent the current document selection from
// being written to the clipboard.
e.preventDefault();
});
""",
[Output("copy_output", "children")],
[Input("copy_button", "n_clicks")],
[State("excel_output", "data")]
)
您必须为此使用套接字,使用 sockets 服务器可以触发前端。 推送通知也用于向前端发送消息,但在您的情况下,sockets 是最佳解决方案。 请参考以下答案。 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.