electron调用python_Python on Electron框架

1586010002-jmsa.png

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

I've already done the app in Python, so I want to know if is possible to write cross-platform desktop applications using Python on the Electron framework?

解决方案

It is possible to work with Electron but if you are looking for "webbish" UI capabilities, you can check Flexx - it allows you to code in pure Python but still use the styling and UI flexibility of web development tools.

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

First make sure you have everything installed:

pip install Flask

npm install electron-prebuilt -

npm install request-promise -g

Now create the directory where you want all the magic to happen and include 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 basic 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 post itself - are the following notes

Notice that in main.js, we spawn a child process for a Python application. Then we check whether the server has been up or not using unlimited loop (well, bad practice! we should actually check the time required and break the loop after some seconds). After the server has been up, we build an actual electron window pointing to the new local website index page.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值