无法在我的Electron应用程序中捆绑Python代码。

huangapple go评论64阅读模式
英文:

Can't bundle a python code in my electron app

问题

抱歉,你的请求不明确,无法理解你要求的内容。请提供更具体的信息或问题,我将尽力提供帮助。

英文:

I'm new to electron, so I would like to be able to create an installer for my electron software but when I do a make or package with electron-forge, my python program doesn't start. This is due to the fact that I have not packaged the python program except that I don't know how to do it ? and I'm a little bit confused about the solutions to use according to my own files (extraResources, asar...).

I also tried electron-builder but the page that opened after the build was empty, it was displayed in the console:

app.js:1 Failed to load resource: net::ERR_FILE_NOT_FOUND

With the npm run make command, I get this error:

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)

With the npm run package command, the .exe is created and runs but no value is displayed and in the console I get the following error:

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

Here are the code :

package.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"
    }
  }
}

Main.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)

App.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> 

  );
}

Python :

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()

Regards,

答案1

得分: 1

我按照这些帖子来集成我的Python代码,它已经成功工作:https://stackoverflow.com/questions/67146654/how-to-compile-python-electron-js-into-desktop-app-exe

英文:

I followed these post to integrate my python code and it have worked :https://stackoverflow.com/questions/67146654/how-to-compile-python-electron-js-into-desktop-app-exe

huangapple
  • 本文由 发表于 2023年1月10日 19:16:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75069153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定