【问题标题】:How to call brython/python from javascript?如何从javascript调用brython/python?
【发布时间】:2020-09-04 06:49:32
【问题描述】:

我正在尝试从 javascript 调用 brython 函数承诺它会给出错误 ReferenceError: brythonListener is not defined这个怎么解决?

python/brython 代码

<script type="text/python">
def execute(*args):
    print(str(args))
window.brythonListener=execute
</script>

javascript 代码

            function(data){
                console.log(data) //till here code works
                brythonListener(data)
            }
        )

我在这里缺少什么?

【问题讨论】:

    标签: javascript python promise brython


    【解决方案1】:

    ReferenceError: brythonListener is not defined 问题的原因是 brythonListener 是在加载 brython 后创建的,以解决此问题,只需在 js 脚本调用 python 脚本时重新加载 brython

    布莱顿脚本

    <script type="text/python">
    from browser import window
    
    def execute(*args):
        print(str(args))
    
    window.brythonListener = execute
    </script>
    

    调用brython函数的Js脚本

    <Script onload="brython()">
    function(data){
      console.log(data)
      brythonListener(data)
    })
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      相关资源
      最近更新 更多