├── py ├── .env │ ├── bin │ │ ├── python3 │ │ ├── conda │ │ ├── python │ │ ├── activate │ │ └── deactivate │ └── pyvenv.cfg └── api.py ├── index.html ├── package.json ├── render.js └── main.js /py/.env/bin/python3: -------------------------------------------------------------------------------- 1 | python -------------------------------------------------------------------------------- /py/.env/bin/conda: -------------------------------------------------------------------------------- 1 | /Users/dustni/anaconda3/bin/conda -------------------------------------------------------------------------------- /py/.env/bin/python: -------------------------------------------------------------------------------- 1 | /Users/dustni/anaconda3/bin/python -------------------------------------------------------------------------------- /py/.env/bin/activate: -------------------------------------------------------------------------------- 1 | /Users/dustni/anaconda3/bin/activate -------------------------------------------------------------------------------- /py/.env/bin/deactivate: -------------------------------------------------------------------------------- 1 | /Users/dustni/anaconda3/bin/deactivate -------------------------------------------------------------------------------- /py/.env/pyvenv.cfg: -------------------------------------------------------------------------------- 1 | home = /Users/dustni/anaconda3/bin 2 | include-system-site-packages = false 3 | version = 3.6.0 4 | -------------------------------------------------------------------------------- /py/api.py: -------------------------------------------------------------------------------- 1 | import zerorpc 2 | 3 | class HelloRPC(object): 4 | def hello(self, name): 5 | return "Hello, %s" % name 6 | 7 | s = zerorpc.Server(HelloRPC()) 8 | s.bind("tcp://0.0.0.0:4242") 9 | s.run() -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Hello XX 7 | 8 | 9 | 10 |

11 | 12 | 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-python", 3 | "version": "0.1.0", 4 | "main": "main.js", 5 | "scripts": { 6 | "start": "electron .", 7 | "build-python": "pyinstaller ./py/api.py --clean --distpath ./pydist", 8 | "pack-app": "./node_modules/.bin/electron-packager . --overwrite --ignore=py$" 9 | }, 10 | "devDependencies": { 11 | "electron": "^2.0.2", 12 | "electron-packager": "^12.1.0" 13 | }, 14 | "dependencies": { 15 | "request-promise": "^4.2.2", 16 | "zerorpc": "^0.9.7" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /render.js: -------------------------------------------------------------------------------- 1 | // renderer.js 2 | 3 | var zerorpc = require("zerorpc"); 4 | var client = new zerorpc.Client(); 5 | client.connect("tcp://127.0.0.1:4242"); 6 | 7 | let name = document.querySelector('#name') 8 | let result = document.querySelector('#result') 9 | name.addEventListener('input', () => { 10 | client.invoke("hello", name.value, (error, res) => { 11 | if(error) { 12 | console.error(error) 13 | } else { 14 | result.textContent = res 15 | } 16 | }) 17 | }) 18 | name.dispatchEvent(new Event('input')) 19 | 20 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | const {app, BrowserWindow} = require('electron') 2 | 3 | // Keep a global reference of the window object, if you don't, the window will 4 | // be closed automatically when the JavaScript object is garbage collected. 5 | let win 6 | 7 | function createWindow () { 8 | win = new BrowserWindow({width: 800, height: 600}) 9 | win.loadFile('index.html') 10 | win.webContents.openDevTools() 11 | // 当 window 被关闭,这个事件会被触发。 12 | win.on('closed', () => { 13 | win = null 14 | }) 15 | } 16 | 17 | // Electron 会在初始化后并准备 18 | // 创建浏览器窗口时,调用这个函数。 19 | // 部分 API 在 ready 事件触发后才能使用。 20 | app.on('ready', createWindow) 21 | 22 | // 当全部窗口关闭时退出。 23 | app.on('window-all-closed', () => { 24 | // 在 macOS 上,除非用户用 Cmd + Q 确定地退出, 25 | // 否则绝大部分应用及其菜单栏会保持激活。 26 | if (process.platform !== 'darwin') { 27 | app.quit() 28 | } 29 | }) 30 | 31 | app.on('activate', () => { 32 | // 在macOS上,当单击dock图标并且没有其他窗口打开时, 33 | // 通常在应用程序中重新创建一个窗口。 34 | if (win === null) { 35 | createWindow() 36 | } 37 | }) 38 | 39 | // add these to the end or middle of main.js 40 | const path=require('path') 41 | 42 | let pyProc = null 43 | let pyPort = null 44 | 45 | 46 | 47 | const createPyProc = () => { 48 | let port = '4242' 49 | // let script = path.join(__dirname, 'py', 'api.py') 50 | let script = path.join(__dirname, 'pydist', 'api','api') 51 | pyProc = require('child_process').execFile(script, [port]) 52 | // pyProc = require('child_process').spawn('python', [script, port]) 53 | if (pyProc != null) { 54 | console.log('child process success') 55 | } 56 | } 57 | 58 | const exitPyProc = () => { 59 | pyProc.kill() 60 | pyProc = null 61 | pyPort = null 62 | } 63 | 64 | app.on('ready', createPyProc) 65 | app.on('will-quit', exitPyProc) 66 | --------------------------------------------------------------------------------