【问题标题】:Packaging electron app powered by Python Flask electron由 Python Flask electron 提供支持的打包电子应用程序
【发布时间】:2018-07-18 02:01:01
【问题描述】:

我刚刚创建了一个由 Flask 驱动的电子应用程序。

当我在 powershell 中运行应用程序时效果很好,但是当我使用电子打包器构建这个应用程序时,它成功了,但是应用程序不起作用。

似乎 python 代码不会包含在应用程序中。 如何通过集成我在应用程序中使用的所有 python 代码和模块来构建应用程序?

我正在使用任何 python 模块,例如 pandas

【问题讨论】:

    标签: electron


    【解决方案1】:

    我能够使用来自 here 的指南打包一个电子烧瓶应用程序,该指南提供了下面给出的答案的更好细节。

    首先确保 pyinstaller .exe 在您运行它时确实正确启动了服务器,并且当您在浏览器中定向到托管页面时,该应用程序会执行您需要它执行的所有操作。打包flask app教程是here

    然后确定你什么时候执行:

    var subpy = require('child_process').spawn('./dist/hello/hello');
    

    你确定它:

    var subpy = require('child_process').spawn('path_to_flask_exe');
    

    【讨论】:

      【解决方案2】:

      使用 PyInstaller 构建烧瓶应用程序。您可以通过 google 找到关于它的各种教程。选择一个适合您需要的。总是很高兴阅读官方文档https://www.pyinstaller.org/。 好吧,我不知道您创建电子入口点的方法。我所做的是,在入口点(对我来说通常是 main.js)我创建了一个在应用程序上调用的函数已准备就绪。我从Python on Electron frameworkhttps://github.com/fyears/electron-python-example 得到的一些东西

      main.js

      'use strict';
      
      const electron = require('electron');
      const app = electron.app;
      const BrowserWindow = electron.BrowserWindow;
      const path = require('path');
      
      // This method will be called when Electron has finished
      // initialization and is ready to create browser mainWindow.
      // Some APIs can only be used after this event occurs.
      
      var mainWindow = null;
      
      function createWindow(){
        // spawn server and call the child process
        var rq = require('request-promise');
        mainAddr = 'http://localhost:4040/'
      
        // tricks 1 worked for me on dev.. but building installer of electron 
        // server never started.. didn't find time to fixed that       
        // var child = require('child_process').spawn('python', 
        //                                               ['.path/to/hello.py']);
        // or bundled py
        // var child = require('child_process').spawn('.path/to/hello.exe');
      
        // tricks 2, a little variation then spawn :)
        var executablePath = './relative/path/to/your/bundled_py.exe';
        var child = require('child_process').execFile;
      
        child(executablePath, function(err, data) {
          if(err){
            console.error(err);
            return;
          }
          console.log(data.toString());
        });
      
        // Create the browser mainWindow
        mainWindow = new BrowserWindow({
          minWidth: 600,
          minHeight: 550,
          show: false
        });
      
        // Load the index page of the flask in local server
        mainWindow.loadURL(mainAddr);
      
        // ready the window with load url and show
        mainWindow.once('ready-to-show', () => {
          mainWindow.show();
        });
      
        // Quit app when close
        mainWindow.on('closed', function(){
          mainWindow = null;
          // kill the server on exit
          child.kill('SIGINT');
        });
        // (some more stuff, eg. dev tools) skipped... 
      };
      
      var startUp = function(){
        rq(mainAddr)
          .then(function(htmlString){
            console.log('server started!');
            createWindow();
          })
          .catch(function(err){
            //console.log('waiting for the server start...');
            startUp();
          });
      };
      
      app.on('ready', startUp)
      
      app.on('quit', function() {
          // kill the python on exit
          child.kill('SIGINT');
      });
      
      app.on('window-all-closed', () => {
          // quit app if windows are closed
          if (process.platform !== 'darwin'){
              app.quit();
          }
      });
      

      【讨论】:

      • 提供代码或终端/控制台命令示例以及文本说明可能会有很大帮助。也非常欢迎参考文档。
      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 2021-01-05
      • 2014-11-14
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2019-07-29
      • 2021-10-14
      相关资源
      最近更新 更多