Javascript 在electron应用程序内部编译Python运行时

Javascript 在electron应用程序内部编译Python运行时,javascript,python,node.js,npm,electron,Javascript,Python,Node.js,Npm,Electron,我正在同时使用electron和python。我已经浏览了所有youtube视频和论坛,我可以找到关于使用python后端打包应用程序的主题。这些教程中的许多已经过时,不再有效 到目前为止,这是我的过程。任何帮助都将不胜感激 使用的软件 编辑器:VSCode(主代码正在运行) •在electron应用程序中设置python虚拟环境 $python3-m venv env $source env/bin/activate •安装电子设备 cd项目文件夹 $npm init-y $npm安装elec

我正在同时使用electron和python。我已经浏览了所有youtube视频和论坛,我可以找到关于使用python后端打包应用程序的主题。这些教程中的许多已经过时,不再有效

到目前为止,这是我的过程。任何帮助都将不胜感激

使用的软件 编辑器:VSCode(主代码正在运行)

•在electron应用程序中设置python虚拟环境

$python3-m venv env

$source env/bin/activate

•安装电子设备

cd项目文件夹

$npm init-y

$npm安装electron--保存开发--保存精确版本

•更改
package.json
以反映这些行

{
  "main": "main.js",            // Make sure that is main.js
  "scripts": {
    "start": "electron ."       // Update this
  },
}
•安装pyinstaller以生成python脚本的可执行版本

$pip安装pyinstaller
…然后导航到env/include文件夹

$pyinstaller-F MainPythonFile.py

print('Hello StackOverflow')
•为Mac OS编译应用程序

$npm安装electron forge--保存开发人员

$npx@electron forge/cli导入

$npm start
(以确保其工作正常)

$npm run make
(编译应用程序)

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>APP WINDOW</title>
    <link rel="stylesheet" href="../assets/css/main.css">
</head>
<body>
<div id="main">
    <div id="TopRow">
        <div>
            <div id="window-title-container" > 
                <h2>Interface Test</h2>
            </div>
        </div>  
    </div>
    <div id="row1">
        <div id="left-container">
        </div>
        <div id="right-container">   
            <button id="DlgBtn">Browse</button>
            <button id="CloseBtn">Close</button>
        </div>
    </div>
</div>
<script src="index.js"></script>
</body>
</html>
main.js


function createWindow () {
    // Create the browser window.
    const win = new BrowserWindow({
        height: 375,
        width: 500,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }
    })

    win.loadFile('src/index.html')

    // Open the DevTools.
    win.webContents.openDevTools()

}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {if (process.platform !== 'darwin') {app.quit()}})

app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
})
const DlgBtn = document.getElementById('DlgBtn')
const CloseBtn = document.getElementById('CloseBtn')

// Dialog Button opens a file dialog and then proceeds to process the python scripts
DlgBtn.addEventListener(type ='click', function() {
  const { dialog } = require('electron').remote;
  const path = require('path');
  var fs = require('fs');
  // Synchronous method. This way the dialog can complete before the code moves on.
  let dirs = dialog.showOpenDialogSync({properties:["openDirectory"]});
  // In the actual functionality I pass a folder path to the pyhton script through JSON
  fs.writeFileSync(path.resolve(__dirname, 'PythonParams.json'), JSON.stringify({"selected_dir":dirs}));
  var python = require('child_process').spawn('python', ['./src/hello.py']);
  python.stdout.on('data',function(data){console.log("data: ",data.toString('utf8'))})
})

// Close Button to close interface  
CloseBtn.addEventListener(type ='click', function() {
  window.close()
})
index.js


function createWindow () {
    // Create the browser window.
    const win = new BrowserWindow({
        height: 375,
        width: 500,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }
    })

    win.loadFile('src/index.html')

    // Open the DevTools.
    win.webContents.openDevTools()

}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {if (process.platform !== 'darwin') {app.quit()}})

app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
})
const DlgBtn = document.getElementById('DlgBtn')
const CloseBtn = document.getElementById('CloseBtn')

// Dialog Button opens a file dialog and then proceeds to process the python scripts
DlgBtn.addEventListener(type ='click', function() {
  const { dialog } = require('electron').remote;
  const path = require('path');
  var fs = require('fs');
  // Synchronous method. This way the dialog can complete before the code moves on.
  let dirs = dialog.showOpenDialogSync({properties:["openDirectory"]});
  // In the actual functionality I pass a folder path to the pyhton script through JSON
  fs.writeFileSync(path.resolve(__dirname, 'PythonParams.json'), JSON.stringify({"selected_dir":dirs}));
  var python = require('child_process').spawn('python', ['./src/hello.py']);
  python.stdout.on('data',function(data){console.log("data: ",data.toString('utf8'))})
})

// Close Button to close interface  
CloseBtn.addEventListener(type ='click', function() {
  window.close()
})
hello.py

print('Hello StackOverflow')
package.json

{
  "name": "DEMO5",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "10.1.5"
  }
}

在我使用任何打包程序(electron packager、electron forge等)编译之前,它都可以正常工作。就python而言,它什么都不做。我不熟悉前端开发,非常感谢在这方面的指导/帮助

您不使用的原因是什么?您不使用的原因是什么?