Python on Electron framework

I am trying to write a cross-platform desktop application using web technologies (HTML5, CSS and JS). I took a look at some of the frameworks and decided to use the Electron infrastructure.

I already made this application in Python, so I want to know if it is possible to write cross-platform desktop applications using Python on the Electron framework?

thanks

+13
source share
4 answers

You can work with Electron, but if you are looking for the "webbish" user interface features, you can check Flexx - this allows you to code pure Python, but at the same time use the flexibility of styles and UI of web development tools.

If you insist on going for Electron, you should follow the idea of ​​this post .

First, make sure that you have everything installed:

pip install Flask npm install electron-prebuilt - npm install request-promise -g 

Now create a directory in which you want all the magic to happen and include the following files.

Create your hello.py :

 from __future__ import print_function import time from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World! This is powered by Python backend." if __name__ == "__main__": print('oh hello') #time.sleep(5) app.run(host='127.0.0.1', port=5000) 

Create your base package.json :

 { "name" : "your-app", "version" : "0.1.0", "main" : "main.js", "dependencies": { "request-promise": "*", "electron-prebuilt": "*" } } 

Finally, create your main.js :

 const electron = require('electron'); const app = electron.app; const BrowserWindow = electron.BrowserWindow; electron.crashReporter.start(); var mainWindow = null; app.on('window-all-closed', function() { //if (process.platform != 'darwin') { app.quit(); //} }); app.on('ready', function() { // call python? var subpy = require('child_process').spawn('python', ['./hello.py']); //var subpy = require('child_process').spawn('./dist/hello.exe'); var rq = require('request-promise'); var mainAddr = 'http://localhost:5000'; var openWindow = function(){ mainWindow = new BrowserWindow({width: 800, height: 600}); // mainWindow.loadURL('file://' + __dirname + '/index.html'); mainWindow.loadURL('http://localhost:5000'); mainWindow.webContents.openDevTools(); mainWindow.on('closed', function() { mainWindow = null; subpy.kill('SIGINT'); }); }; var startUp = function(){ rq(mainAddr) .then(function(htmlString){ console.log('server started!'); openWindow(); }) .catch(function(err){ //console.log('waiting for the server start...'); startUp(); }); }; // fire! startUp(); }); 

Taken from the record itself - these are the following notes

Note that in main.js we are starting a child process for a Python application. Then we check whether the server was turned on or not using an unlimited loop (well, bad practice! We must check the time and break the loop in a few seconds). After the server has been inserted, we create an actual electronic window pointing to the new website index page.

+22
source

You can use nodejs modules inside Electron. Check out https://github.com/JeanSebTr/node-python . I have no personal experience, but it may be what you are looking for.

+24
source

You can use python-shell to communicate between Python and Node.js / Electron.

python-shell provides an easy way to run Python scripts from Node.js with basic and efficient interprocess communication and better error handling.

Using python-shell, you can:

  • spawn Python scripts in a child process;
  • switch between text, JSON and binary modes;
  • use custom parsers and formatters;
  • perform data transfer through stdin and stdout streams;
  • Get a stack trace when an error occurs.

In your terminal, make sure that you are in the root folder of your project, and run the following command to install python-shell from npm:

 npm install --save python-shell 

Then you can simply start the Python shell using:

 var pyshell = require('python-shell'); pyshell.run('hello.py', function (err, results) { if (err) throw err; console.log('hello.py finished.'); console.log('results', results); }); 

See more information from this lesson.

0
source

I searched for alternatives to electron using python and found CEF Python for its Python bindings for the Chromium Embedded Framework. CEF is a kind of electron for Python https://github.com/cztomczak/cefpython

0
source

All Articles