Make a desktop app with Python with Electron

Introduction

logo_horizontal.png

When I thought that the GUI environment of Python was delicate, I could easily link with Electron and it seemed that I could create a beautiful GUI environment, so I made a simple application. Reference: Electron as GUI of Python Applications

Thank you for your cooperation!

If you find this article helpful, I would be grateful if you could like this article.

About the author

02.jpg

I am ** Kikagaku Co., Ltd. ** Representative Director ** Ryosuke Yoshizaki ** My name is. Currently, "** Machine Learning / Artificial Intelligence De-Black Box Seminar **" and "** Machine Learning Online Tutor ** ”is operated.

Biography

Affiliation Department / Department research content Punishment
Maizuru National College of Technology Department of Electronic Control Engineering Study image processing (AR)
Maizuru National College of Technology Department of Electrical and Control Systems Engineering Research on robotics, system control, and optimization
Kyoto University Graduate School Graduate School of Informatics (Kano Lab Appliedresearchonmachinelearningforthemanufacturingindustry ADCHEM2016BestPaperAward,ChemicalEngineeringSocietyTechnologyAward
SHIFT Inc. President's office Research on software test automation by artificial intelligence CEDEC 2016 stage
Carat Co., Ltd. Director and COO Optimal itinerary proposal app (natural language processing / optimization)
Kikagaku Co., Ltd. PresidentandCEO Machinelearning/artificialintelligenceseminarOrOnlinetutor

Kikagaku Co., Ltd.

logo_horizontal.png

Providing educational services for machine learning and artificial intelligence

We look forward to your follow-up

We provide information on machine learning and artificial intelligence from a business perspective and recommended reference books.

President and CEO Ryosuke Yoshizaki Twitter:@yoshizaki_kkgk Facebook:@ryosuke.yoshizaki Blog: Blog of Kikagaku representative

Now that the introduction is long, let's start building the environment!

Development environment

・ OS: Mac OS X EL Capitan (10.11.5)

Environment

This time we'll use Flask, Python's lightweight web framework.

Flash(python)And Electron installation


$ pip install Flask
$ npm install -g electron-prebuilt

If you stumbled on installing Electron, updating node.js to the latest version is likely to work. For details, see the previous article Introduction to Electron (from installation to Hello World).

Create Python desktop apps with Electron

Create a new project

Directory structure


PythonApp
├── node_modules
├── hello.py
├── main.js
└── package.json

Create a new project for Electron.

Create a new project


$ mkdir PythonApp
$ cd PythonApp
$ npm init -y

Here, there is a required module on the node.js side, so install it.

Installation of required modules


$ npm install --save request
$ npm install --save request-promise 

npm init -package created with y.Edit the json.




#### **`package.json`**
```json

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

Create main.js of application control part

Create main.js in the directory to control Electron.

main.js


//Initial setting on Electron side
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let mainWindow;

//Quit when the app is closed
app.on('window-all-closed', function() {
  app.quit();
});

//Processing after launching the app
app.on('ready', function() {
  var subpy = require('child_process').spawn('python',['./hello.py']);
  var rq = require('request-promise');
  var mainAddr = 'http://localhost:5000';

  var openWindow = function() {
    mainWindow = new BrowserWindow({width: 400, height: 300 });
    mainWindow.loadURL(mainAddr);

    //End processing
    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) {
        startUp();
      });
  };

  startUp();
});

Create hello.py for the application display part

Create hello.py.

hello.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import time
from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():
    return "Hello World!<br>This is powered by Python backend."

if __name__ == "__main__":
    print('on hello')
    app.run(host="127.0.0.1", port=5000)

Run Electron

Run the sample application.

Run Electron


$ electron .
スクリーンショット 2016-08-07 2.01.18.png

It worked fine. That's all there is to it.

Thank you for your hard work.

We look forward to your follow-up

We provide information on machine learning and artificial intelligence from a business perspective and recommended reference books.

President and CEO Ryosuke Yoshizaki Twitter:@yoshizaki_kkgk Facebook:@ryosuke.yoshizaki Blog: Blog of Kikagaku representative

Until the end Thank you for reading.

Recommended Posts

Make a desktop app with Python with Electron
Make a fortune with Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Let's make a GUI with python.
Let's make a graph with python! !!
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Make a scraping app with Python + Django + AWS and change jobs
Make a Twitter trend bot with heroku + Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Make one repeating string with a Python regular expression.
Try to make a command standby tool with python
[Let's play with Python] Make a household account book
I made a net news notification app with Python
Let's make a simple game with Python 3 and iPhone
Make a breakpoint on the c layer with python
Make Puyo Puyo AI with Python
Make a bookmarklet in Python
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app
Create a directory with python
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
Make a fire with kdeplot
Embed a Python interpreter into a C ++ app with pybind11 + cmake
Associate Python Enum with a function and make it Callable
Experiment to make a self-catering PDF for Kindle with Python
Make a Mac menu bar resident weather app with rumps!
Create a simple Python development environment with VSCode & Docker Desktop
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Make apache log csv with python
Make a sound with Jupyter notebook
Solve ABC166 A ~ D with Python
Let's make a breakout with wxPython
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
python / Make a dict from a list.
Creating a simple app with flask
Solve ABC168 A ~ C with Python
[Python] Make the function a lambda function
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Make a filter with a django template
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a supercomputer with xCAT
Make a model iterator with PySide
[Python] Inherit a class with class variables
I made a daemon with Python