Plotly-Dash:如何确定客户端回调中的触发输入 [英] Plotly-Dash: How to determine trigger input in client-side callback

查看:106
本文介绍了Plotly-Dash:如何确定客户端回调中的触发输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dash 的文档描述了在服务器端回调的情况下如何确定哪个输入触发了回调:Advanced回调.

Dash's documentation describes how to determine which input triggered a callback in the case of server-side callbacks: Advanced Callbacks.

有没有办法确定哪个输入触发了客户端回调?

Is there a way to determine which input triggered a client-side callback?

看起来这个功能是在 1.13.0 版本中添加的(#1240),但我的浏览器控制台显示 dash_clientside.callback_contextundefined.我正在运行 Dash 版本 1.19.0.

It looks like this functionality was added in version 1.13.0 (#1240), but my browser console indicates that dash_clientside.callback_context is undefined. I am running Dash version 1.19.0.

我遇到的错误是由于我的 Dash 安装出现问题.我是通过 conda install dash 安装的,Anaconda 主频道上的 Dash 包似乎有问题.从 conda forge 安装解决了这个问题:conda install -c conda-forge dash.请参阅已接受的工作示例的答案.

The error I was experiencing was due to an issue with my Dash installation. I had installed via conda install dash, and it appears the Dash packages on the Anaconda main channel have issues. Installing from conda forge fixed the problem: conda install -c conda-forge dash. See accepted answer for a working example.

推荐答案

是的,这是可能的.正如您自己所注意到的,信息在 dash_clientside.callback_context 变量中可用.这是一个小例子,

Yes, that is possible. As you note yourself, the information is available in the dash_clientside.callback_context variable. Here is a small example,

import dash_html_components as html
from dash import Dash
from dash.dependencies import Output, Input

# Create app.
app = Dash(prevent_initial_callbacks=True)
app.layout = html.Div([
    html.Button("Button 1", id="btn1"), html.Button("Button 2", id="btn2"), html.Div(id="log")
])
app.clientside_callback(
    """
    function(x, y){
        const triggered = dash_clientside.callback_context.triggered.map(t => t.prop_id);
        return "Hello from [" + triggered + "]";
    }
    """, Output("log", "children"), [Input("btn1", "n_clicks"), Input("btn2", "n_clicks")])

if __name__ == '__main__':
    app.run_server()

在 Linux Mint 20 上的 python 3.8 上使用 dash==1.18.1 测试.

tested with dash==1.18.1 on python 3.8 on Linux Mint 20.

这篇关于Plotly-Dash:如何确定客户端回调中的触发输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆