gpt4 book ai didi

javascript - 如何在 python 中调用 Javascript 函数?

转载 作者:行者123 更新时间:2023-12-02 21:24:17 24 4
gpt4 key购买 nike

我正在尝试使用 Electron 和深度强化学习制作一个贪吃蛇游戏。我用 python 做强化学习,用 Javascript 做游戏。现在我怎样才能在Python中调用这样的函数呢?

makeSomeThing(x) {

}

getValue() {
return x;
}

最佳答案

  1. 请将您的 Python 脚本构建为可执行二进制文件。您可以使用pyinstaller将 python 脚本打包到独立的可执行文件中。

  2. 然后你可以像这样在你的 Electron 项目中生成这个二进制文件。

    import { spawn } from 'child_process';
    // in my case I'm storing the file at bin directory at the root path of the application
    // You can change this whatever you want

    const pythonPath = const basicURL = process.env.NODE_ENV === 'development'
    ? path.join(__dirname, './bin/xxxx')
    : path.join(process.resourcesPath, 'bin', 'xxxx');

    const params = ['arg1', 'arg2']; // params that your python scripts need.
    const pythonChildProcess = spawn(pythonPath, params);
    pythonChildProcess.stdout.on('data', data => {
    console.log(`stdout: ${data}`);

    // Here is where the output goes
    });
    pythonChildProcess.stderr.on('data', data => {
    console.log(`tderr: ${data}`);

    // Here is where the error output goes
    });
    pythonChildProcess.on('close', code => {
    console.log(`closing code: ${code}`);
    // Here you can get the exit code of the script
    });

关于javascript - 如何在 python 中调用 Javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60776975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com