【问题标题】:client side callback is not working in dash plotly客户端回调在破折号中不起作用
【发布时间】:2021-08-23 02:48:53
【问题描述】:

您好,我正在尝试在 JS 中使用提示符获取输入,该输入稍后会传递到常规回调中,然后打印在屏幕上,这是我的破折号代码和 custom-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 应用程序时,提示即客户端脚本根本不起作用,并且我收到常规回调错误,例如回调输入为无,请帮助我

错误信息: ** 在输出的回调中: 错误数据 没有Input 元素。 没有Input 元素,它永远不会被调用。

订阅Input 组件将导致 每当它们的值发生变化时调用回调。 **

【问题讨论】:

    标签: javascript callback plotly-dash


    【解决方案1】:

    问题是,正如错误所说,您的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 函数中返回对象,因为您的 error_text 回调输出到不接受对象的 children

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-16
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多