Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Using Web technologies, I've tried to create a cross-platform desktop application. Then when I started to look at a few frameworks, I decided to utilize the Electron framework.

Previously, I also did an application in python, So  I just need to know using python whether it is possible to write a cross platform desktop app with the help of the Electron framework?

1 Answer

0 votes
by (26.4k points)

It is possible using Electron but if you're seeking "webbish" UI capabilities, you can also use it. Because It uses the same styling and UI flexibility of web development tools but still it allows you to code in pure python.

Ensure that you've installed everything:

pip install Flask

npm install electron-prebuilt -

npm install request-promise -g

Now create the directory, where you will include all the following files

Create 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 basic package.json:

{

  "name"    : "your-app",

  "version" : "0.1.0",

  "main"    : "main.js",

  "dependencies": {

    "request-promise": "*",

    "electron-prebuilt": "*"

  }

}

Create 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();

});

Looking for a good python tutorial course? Join the python online course to gain more knowledge in python.

Browse Categories

...