如何从 javascript 调用 brython/python?

How to call brython/python from javascript?

我正在尝试从 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)
            }
        )

我在这里缺少什么?

问题的原因 ReferenceError: brythonListener is not definedbrythonListener是在加载brython之后创建的,以解决这个问题只需在js脚本调用python脚本

时重新加载brython

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>

我编写了一个add-on,让你在分析和查看Brython的代码和基本工作方法后能够做到这一点......

<script>
runconsole_scripts = $B.parser._run_scripts
function check_all_old_brython()
{
    $("script[type='text/python']").attr("type","text/python/old")

};

function add_new_script(text)
{
    $(function () {
        $('<script>')
          .attr('type', 'text/python')
          .text(text)
          .appendTo('body');
      });
};

function remove_all_new_brython_scripts()
{
    $("script[type='text/python']").remove()
};

function uncheck_all_old_brython()
{
    $("script[type='text/python/old']").attr("type","text/python")
};

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

async function run_exec(code, options)
{
    check_all_old_brython();
    add_new_script(code);
    $B.parse_options(options);
    if (!($B.isWebWorker || $B.isNode)) {
        //_run_scripts(options)
        await sleep(1)
        runconsole_scripts(options);
        await sleep(1)
    };
    
    remove_all_new_brython_scripts();
    uncheck_all_old_brython();
};
</script>

你必须在调用库后把代码放在正确的地方....

<script src="assets/js/brython.min.js?h=d56c14be45278a1455b1f545237583fc"></script>
<script src="assets/js/brython_stdlib.js?h=e1293c1c0a36696318d3804d6240f20d"></script>
<script>
runconsole_scripts = $B.parser._run_scripts
function check_all_old_brython()
{
    $("script[type='text/python']").attr("type","text/python/old")

};

function add_new_script(text)
{
    $(function () {
        $('<script>')
          .attr('type', 'text/python')
          .text(text)
          .appendTo('body');
      });
};

function remove_all_new_brython_scripts()
{
    $("script[type='text/python']").remove()
};

function uncheck_all_old_brython()
{
    $("script[type='text/python/old']").attr("type","text/python")
};

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

async function run_exec(code, options)
{
    check_all_old_brython();
    add_new_script(code);
    $B.parse_options(options);
    if (!($B.isWebWorker || $B.isNode)) {
        //_run_scripts(options)
        await sleep(1)
        runconsole_scripts(options);
        await sleep(1)
    };
    
    remove_all_new_brython_scripts();
    uncheck_all_old_brython();
};
</script>

或正文标签后....

我建议您将代码放在一个文件中并将其命名为“brython_call_from_console.js”