Python backend for Electron


#1

Hi all,

I’ve been learning python over the last couple of months next to my basic knowledge of CSS & HTML. I was looking into the possibility to create a very basic desktop app using the Electron framework. However, I can’t seem to figure out how to “connect” both.

I’ve been able to create fully functional python scripts and running them in the shell. However what I wanted to have for this application is a basic GUI (and I was looking at Electron to do it). I can’t seem to find much information though on how to align both so I can interact with Python and vice versa. I’ve found very few information both on google, here and stackoverflow.

Would anybody be able to point me in the right direction? Or is what I ask impossible? Should I focus more on learning JavaScript (which I am currently doing only the basics though) and code the app using JS?

As it’s my first post, apologies if not appropriate! I did read the rules. :wink:

Many thanks in advance!


#2

What you ask is not impossible but it also isn’t something that is supported by the Electron framework. Electron supports JavaScript out of the box and there are tools such as electron-compile that add support for other JavaScript-based languages.

If you want to write code for Electron in Python, you’ll have to either find or create a tool that will convert Python code to JavaScript or create some other kind of bridge between the two on your own.


#3

Maybe you want this?


#4

The easiest solution I’ve found is to connect the two via a local websocket.

You can use the python socket library and javascript has the WebSocket() function.

The biggest challenge here is navigating the websocket protocol on the python side (js does a lot of it for you) - there’s a handshake protocol for connection, a frame header that needs to be appended to all data you want to send, and you need to unmask data received.

Best of luck!


#5

I would use tornado as the back-end framework and it has really great support for web sockets.

If you need something with pre-built widgets you could try flexx https://github.com/zoofIO/flexx or maybe Bokeh. Which are built on top of tornado.


#6

#7

I can’t recall which of the above links I hacked this helper function together from, but it might get someone looking in the right direction. I’m using it to run arbitrary python scripts from an Electron app in Ubuntu. Haven’t tried to compile for other OS, so can’t attest to that.

const { dialog } = require('electron').remote;
const spawn = require('child_process').spawn;

function doPy(pycommand, args) {
  if (args === undefined) {
    console.log('No Args!');
  } else {
    // Using spawn to call python script
    // Put command + args into an array to be used below
    if (Array.isArray(args)) {
      args.unshift(pycommand);
    } else {
      args = [pycommand].concat(args);
    }

    const pyspawn = spawn(
      'python3', args
    );

    pyspawn.stdout.on('data', function pyspawnStdout(data) {
      console.log(`stdout: ${data}`); // Must flush the stdout from pyscript to fire an event in node
    });

    pyspawn.stderr.on('data', function pyspawnStderr(data) {
      console.log(`stderr: ${data}`);
    });

    pyspawn.on('exit', function pyspawnExit(code) {
      if (code !== 0) {
        console.log(`Failed: ${code}`);
      }
    });
  }
}

Then I can use doPy(command, args) wherever I need.

Test thoroughly. I make no guarantees.