Skip to content

Instantly share code, notes, and snippets.

@Miraj50
Last active February 22, 2024 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miraj50/f2391b92b4dba57fe7e1860bfa12565f to your computer and use it in GitHub Desktop.
Save Miraj50/f2391b92b4dba57fe7e1860bfa12565f to your computer and use it in GitHub Desktop.
Minimal Electron App with Python backend
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()
})
{
"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"
}
}
<script src="weather.js"></script>
<button onclick="get_weather()">Go!</button>
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);
})
}
import sys
data=""
for line in sys.stdin:
data+=line.rstrip()
print(data)
sys.stdout.flush()
@Rithwik0604
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment