NodeJS 无法在我的电子应用程序中捆绑python代码

jgovgodb  于 2023-01-12  发布在  Node.js
关注(0)|答案(1)|浏览(120)

我刚接触electron,所以我希望能够为我的electron软件创建一个安装程序,但当我使用electron-forge进行make或打包时,我的python程序无法启动。这是因为我没有打包python程序,只是我不知道如何操作?而且我对根据我自己的文件使用的解决方案有点困惑(额外资源,asar ...)。
我还尝试了electron-builder,但构建后打开的页面是空的,它显示在控制台中:
app.js:1 Failed to load resource: net::ERR_FILE_NOT_FOUND
使用npm run make命令时,我收到以下错误:

An unhandled rejection has occurred inside Forge:
Error: Failed with exit code: 1
Output:
Tentative de cr�ation du package depuis 'electron_react_app.nuspec'.
Authors is required.
at ChildProcess.<anonymous> (C:\Users\cantr\Desktop\Stage ingénieur KOMILFO SPORT\Sail Vision\Electron-React\node_modules\electron-winstaller\lib\spawn-promise.js:49:24)
    at ChildProcess.emit (node:events:365:28)
    at ChildProcess.emit (node:domain:470:12)
    at maybeClose (node:internal/child_process:1067:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)

使用npm run package命令,. exe被创建并运行,但没有显示任何值,并且在控制台中,我得到以下错误:

WebSocket connection to 'ws://localhost:8000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

下面是代码:

    • 包. json**:
{
  "name": "electron-react-app",
  "version": "1.0.0",
  "description": "1. First install dependencies: ```npm install``` </br> 2. In one terminal window run: ```npm run watch``` to compile react code <br/> 3. In other one run: ```npm start``` to start Electron app",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack --config webpack.common.js --watch",
    "start": "electron-forge start",
    "dist": "electron-builder",
    "package": "electron-forge package",
    "make": "electron-forge make"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0",
    "git": "^0.1.5",
    "python-shell": "^3.0.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-table": "^7.8.0",
    "socket.io-client": "^2.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-react": "^7.10.4",
    "@electron-forge/cli": "^6.0.4",
    "@electron-forge/maker-deb": "^6.0.4",
    "@electron-forge/maker-rpm": "^6.0.4",
    "@electron-forge/maker-squirrel": "^6.0.4",
    "@electron-forge/maker-zip": "^6.0.4",
    "babel-loader": "^8.3.0",
    "css-loader": "^4.3.0",
    "electron": "^10.4.7",
    "electron-builder": "^23.6.0",
    "electron-reload": "^1.5.0",
    "sass": "^1.26.11",
    "sass-loader": "^10.0.2",
    "style-loader": "^1.2.1",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  },
  "build": {
    "appId": "SailVision",
    "win": {
      "target": "zip"
    }
  }
}
    • 主要. js**:
const { BrowserWindow, app, ipcMain, Notification } = require('electron');
const path = require('path');
const PythonShell = require('python-shell');

const isDev = !app.isPackaged;

function createWindow() {
  var python = require('child_process').spawn('python', ['./fifo.py']);
  const win = new BrowserWindow({
    width: 1200,
    height: 800,
    backgroundColor: "white",
    webPreferences: {
      nodeIntegration: false,
      worldSafeExecuteJavaScript: true,
      contextIsolation: true,
      preload: path.join(__dirname, 'preload.js')
    }
  })

  win.loadFile('index.html');
}

if (isDev) {
  require('electron-reload')(__dirname, {
    electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
  })
}

app.whenReady().then(createWindow)
    • 应用程序js**:
import React, { useState, useEffect } from 'react';
import Modal from './Modal/Modal'

export default function App() {
  const [data, setData] = useState(null);
  const [show,setShow] = useState(false);

  useEffect(() => {
    const socket = new WebSocket('ws://localhost:8000');

    socket.addEventListener('message', (event) => {
      setData(JSON.parse(event.data));
    });
  }, []);

  return (
            <div className="home">
                <div className="template-1" id="temp1">
                <div className="panel-1">
                    <div className="panel-header">
                    <h1>Panel 1</h1>
                    <i className='bx bx-cog modal-trigger-panel'></i>
                    </div>
                    <div className="panel-body">
                    <div className="sec-5 modal-trigger-data" id="hs-sec-5" onClick={()=>setShow(true)}>
                        {data ? <span class="h1" id="h1-fs-s5">{data[0]}</span> : <span class="h1" id="h1-fs-s5">--</span>}
                        <h2>TWIST</h2>
                        <h3>s5</h3>
                    </div>
                    <div className="sec-4 modal-trigger-data" id="hs-sec-4" onClick={()=>setShow(true)}>
                        {data ? <span class="h1" id="h1-fs-s4">{data[1]}</span> : <span class="h1" id="h1-fs-s4">--</span>}
                        <h2>TWIST</h2>
                        <h3>s4</h3>
                    </div>
                    <div className="sec-3 modal-trigger-data" id="hs-sec-3" onClick={()=>setShow(true)}>
                        {data ? <span class="h1" id="h1-fs-s3">{data[2]}</span> : <span class="h1" id="h1-fs-s3">--</span>}
                        <h2>TWIST</h2>
                        <h3>s3</h3>
                    </div>
                    <div className="sec-2 modal-trigger-data" id="hs-sec-2" onClick={()=>setShow(true)}>
                        {data ? <span class="h1" id="h1-fs-s2">{data[3]}</span> : <span class="h1" id="h1-fs-s2">--</span>}
                        <h2>TWIST</h2>
                        <h3>s2</h3>
                    </div>
                    <div className="sec-1 modal-trigger-data" id="hs-sec-1" onClick={()=>setShow(true)}>
                        {data ? <span class="h1" id="h1-fs-s1">{data[4]}</span> : <span class="h1" id="h1-fs-s1">--</span>}
                        <h2>TWIST</h2>
                        <h3>s1</h3>
                    </div>
                    </div>
                </div>
                </div>
                <Modal onClose={() => setShow(false)} show={show} />
            </div> 

  );
}
    • 巨蟒**:
import asyncio
import random
import datetime
import websockets
import json

async def handler(websocket, path):
    while True:
        #log_decoder()
        data = [random.randint(0, 100) for _ in range(10)]
        await websocket.send(json.dumps(data))
        await asyncio.sleep(1)

start_server = websockets.serve(handler, "localhost", 8000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

此致,

相关问题