5

I just created an electron app which is powered by Flask.

It works nicely when I run the app in powershell, but when I build this app with electron-packager, It succeeds, but the app does not work.

It seems python code would not be included in the app. How can I build the app with integrating all python code and modules I am using in the app?

I am using any python modules like pandas

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
bakuuuuu
  • 51
  • 1
  • 3

2 Answers2

3

I was able to package an electron-flask app using guidelines from here which gives better details of the answer given below.

First make sure that the pyinstaller .exe actually starts the server correctly when you run it and that when you direct to the hosted page in your browser that the app does everything you need it to do. Packaging flask app tutorial is here.

And then make sure when you are executing:

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

That you make sure its:

var subpy = require('child_process').spawn('path_to_flask_exe');
Pelonomi Moiloa
  • 516
  • 5
  • 12
0

Build the flask app with PyInstaller.. You can find various tutorial on it through google.. Pick one that suits your needs. Always good to read the official documentation https://www.pyinstaller.org/ . Well I don't know your approach of creating the electron entry point. what i did was, on the entry point (usually main.js for me) I have created a function which is called on app is ready. Some of the stuffs i got from Python on Electron framework and from https://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();
    }
});
  • Providing code or Terminal/Console command samples along to text explanation may help a lot. References to the docs are also very welcome. – dr.dimitru Mar 14 '18 at 07:38