客户端回调在破折号中不起作用

client side callback is not working in dash plotly

您好,我正在尝试在 JS 中使用提示获取输入,该输入稍后会传递到常规回调中,然后在屏幕上打印出来,这是我的短划线代码和自定义-script.js 代码

dash.py:

import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output,State,ClientsideFunction



app=dash.Dash()
server = app.server
app.layout=html.Div([
    dcc.Store(id='error',data={}),
    html.Div(id='output')
])

app.clientside_callback(
    ClientsideFunction(
        namespace='clientside',
        function_name='error_text'
    ),
    
    Output('error','data')
)

@app.callback(Output('output','children'),
              [Input('error','data')])
def error_text(error):
    return error
    
       
        
if __name__ == '__main__':
    app.run_server(debug=True)

assets/custom-script.js:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
        error_text: function() {
            var error=prompt('enter the error text');
            if(error!==null){
            return {errorText:error};
            }
        }
    }
});

当我 运行 我的 dash 应用程序时,提示即客户端脚本根本不工作,我收到常规回调的错误,例如回调的输入是 none,请帮助我

错误信息: ** 在输出回调中: error.data 没有 Input 个元素。 没有 Input 个元素,它将永远不会被调用。

订阅 Input 个组件将导致 只要它们的值发生变化,就会调用回调。 **

问题是,如错误所述,您的 clientside_callback 没有 Input。就像常规的 Dash 回调一样,客户端回调也需要至少有一个 Input 和一个 Output:

app.clientside_callback(
    ClientsideFunction(namespace="clientside", function_name="error_text"),
    Output("error", "data"),
    Input("error", "data"),
)

以上将在页面加载时起作用,因为如果没有另外指定,dash 会在应用程序启动时运行回调。

现在您可能想要一个与上面示例不同的输入/触发器,但您至少需要一个 Input


此外,您不能在 error_text 函数中 return 对象,因为 error_text 回调输出到 children 不接受对象。

在这种情况下,您可以 return 错误文本原样:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
  clientside: {
    error_text: function () {
      var error = prompt("enter the error text");
      if (error !== null) {
        return error
      }
    },
  },
});