首页 > 解决方案 > 如何构建可以在 Web UI 上运行的 Python 脚本

问题描述

我有一个 Python 脚本,它可以创建一个 excel 文件,其中包含用户从我用几个 html 页面、css 和 javascript 创建的 HTML UI 中提供的输入信息。我需要它离线工作,我需要将它分发给客户。

如何捆绑此 HTML UI 和 Python 文件,以便客户端无需安装 Python 或任何依赖项即可使用我的应用程序?我认为eel这项工作不需要客户端安装 python 来使用它,但是在 eel 中客户端应该已经安装了 chrome 对吗?

编辑:

我的js:

function elect() {
    console.log('Im in function right now');
    let {PythonShell} = require('python-shell');
    var path = require('path');
    var options = {
        scriptPath : path.join(__dirname, '/../engine/')
    }

var axiexe = new PythonShell ('test sort.py', options);

axiexe.on('message', function (message) {
    swal(message);
    })
}

我的目录在E:\Web\testing my app\GUI里面,我有node_modules带有@electron, .bin, python-shelletc 文件夹的文件夹。

标签: pythonelectroneel

解决方案


我现在正在使用 Python 后端提供的 Electron 应用程序。我的流程如下: 1. 将 Pyshell 安装为 npm 模块。2. 创建您的 Electron UI 3. 使用 pyinstaller 编译您的 python 代码。

然后就是诀窍了。Pyshell 实际上也使用机器上安装的 python。另一种方法是在 Python 中运行服务器,我向自己提出了一个问题。如果我有非常琐碎的功能,我为什么要这样做?

解决方案:修改 python-shell 以使其运行 exe 文件,我还使用了一个技巧,我说 python 的路径是我的编译 python_code.exe 并且我的脚本为空,然后我在 python-shell 中注释了检查脚本长度的行在用于构建版本的 communicator.js 中,使用:

let options = {
  mode: 'text',
  pythonPath: "resources/app/python_build/electron_backend.exe"
};
pyshell = new PythonShell(' ', options);

而对于开发,Pyshell 将从 env 中获取您的 Python 安装。变量。所以我们只需要提供一个 .py 文件路径

pyshell = new PythonShell('electron_backend.py');

为了使其运行,请更改 pyshell 模块的源代码,注释如下:

// if (scriptPath.trim().length == 0)
    // throw Error("scriptPath cannot be empty! You must give a script for python to run");

推荐阅读