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')
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.