electron VUE 2+电子+ flask ->Python未在构建时生成

u1ehiz5o  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(351)

我刚刚完成了我的第一个vue+electron+flask项目,我有一个相当困难的时间来尝试打包它。当使用“npm run electron:serve”时,一切都工作得“完美”,但当运行“npm run electron:build”时,我没有得到任何错误,但Flask根本没有启动。我真的不知道如何解决这个问题,我的猜测是,当构建dist文件夹时,到www.example.com的路径app.py是不正确的,但我试图修复它,但运气不好。
下面是background.js代码:

'use strict'

import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // spawn flask app (https://medium.com/red-buffer/integrating-python-flask-backend-with-electron-nodejs-frontend-8ac621d13f72)
  var python = require('child_process').spawn('py', ['../server/app.py']);
  python.stdout.on('data', function (data) {
    console.log("data: ", data.toString('utf8'));
  });
  python.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`); // when error
  });
  
  // Create the browser window.
  const win = new BrowserWindow({
    width: 1500,
    height: 1200,
    webPreferences: {
      
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  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()
})

// 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.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      await installExtension(VUEJS_DEVTOOLS)
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

调用www.example.com的代码的相关部分app.py如下:

async function createWindow() {
  // spawn flask app (https://medium.com/red-buffer/integrating-python-flask-backend-with-electron-nodejs-frontend-8ac621d13f72)
  var python = require('child_process').spawn('py', ['../server/app.py']);
  python.stdout.on('data', function (data) {
    console.log("data: ", data.toString('utf8'));
  });
  python.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`); // when error
  });
  
  // Create the browser window.
  const win = new BrowserWindow({
    width: 1500,
    height: 1200,
    webPreferences: {
      
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })

我试着在app.py路径['.../server/app.py]中放置3个点而不是2个点,以防在创建dist文件夹时需要这个额外的点来找到app.py文件,但这也不起作用。
我的文件夹结构如下:

  • 真空电子
  • 委托人
  • 距离电子
  • 节点模块
  • 公开的
  • 源代码
  • 资产
  • 零部件
  • 路由器
  • 检视
  • App.vue
  • background.js
  • main.js
  • 其他配置文件
  • 伺服器
  • 数据库
  • 环境
  • app.py
  • requirements.txt
  • 导入到www.example.com的其他Python脚本app.py
  • sqlite_portofolio.db

由于这个程序只会被我用在我的个人电脑上,我不想麻烦使用pyInstaller(我想不打包python端会更容易,但如果我错了,请让我知道)。我想有一个electron.exe文件,我可以双击打开电子构建,然后产生Flask服务器。
另外,我的感觉是,我没有正确杀死Flask服务器时关闭应用程序。我认为Flask仍然在运行时关闭电子。我应该做什么,以确保Flask服务器是正确关闭。
没有太多的信息,这些主题,我可以遵循。任何帮助将aprreaciated。

xesrikrc

xesrikrc1#

我也遇到了同样的问题。我点击了这篇文章的链接(https://medium.com/red-buffer/integrating-python-flask-backend-with-electron-nodejs-frontend-8ac621d13f72),它给出了关于杀死python flask服务器的答案。如果你按照这篇文章所说的一切去做,它应该在打开electron.exe时运行后端,但这在我这边没有发生。
编辑:我发现了错误,你需要改变你的spawn上的路径。我建议你在cmd上运行electroniderexe,这样你就可以看到上面的错误,这样你就会看到spawn试图运行的路径。
它可能是:

var python = require('child_process').spawn('py', ['../resources/app/server/app.py']);

你将需要通过[resources/app]访问app.py,因为spawn从电子构建的基本目录开始。
PS:我使用了电子打包器,这就是为什么我的需要添加资源/应用程序,我用pyinstaller在我的后端
希望能对你有所帮助。

相关问题