Last active
February 22, 2024 15:11
-
-
Save Miraj50/f2391b92b4dba57fe7e1860bfa12565f to your computer and use it in GitHub Desktop.
Minimal Electron App with Python backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {app, BrowserWindow} = require('electron') | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
nodeIntegration: true | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('weather.html') | |
// Open the DevTools. | |
mainWindow.webContents.openDevTools() | |
} | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "electron-app", | |
"version": "1.0.0", | |
"description": "A minimal Electron application", | |
"main": "main.js", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"repository": "https://github.com/electron/electron-quick-start", | |
"keywords": [ | |
"Electron", | |
"quick", | |
"start", | |
"tutorial", | |
"demo" | |
], | |
"author": "GitHub", | |
"license": "CC0-1.0", | |
"devDependencies": { | |
"electron": "^9.0.5" | |
}, | |
"dependencies": { | |
"python-shell": "^2.0.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="weather.js"></script> | |
<button onclick="get_weather()">Go!</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { PythonShell } = require('python-shell') | |
function get_weather() { | |
var data = "Cool Weather" | |
let pyshell = new PythonShell('weather.py') | |
pyshell.send(data) | |
pyshell.on('message', function (message) { | |
console.log(message+" HI") | |
}) | |
pyshell.end(function (err, code, signal) { | |
if (err) throw err; | |
console.log(code + ' ' + signal); | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
data="" | |
for line in sys.stdin: | |
data+=line.rstrip() | |
print(data) | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you package this ? I also made a electron app with python, but when i package it, the output folder has my python scripts and .env files which contain my api keys. I used asar as well, but that doesn't do anything to the python scripts.