-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
94 lines (76 loc) · 2.26 KB
/
main.js
File metadata and controls
94 lines (76 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { app, BrowserWindow } = require('electron');
const path = require('path');
const { spawn } = require('child_process');
let pythonProcess = null;
let mainWindow = null;
function startPythonAPI() {
console.log('Starting Python API...');
// Try different Python commands
const pythonCommands = ['python', 'python3', 'py'];
let currentCommand = 0;
function tryStartPython() {
if (currentCommand >= pythonCommands.length) {
console.error('Could not start Python API - no Python interpreter found');
return;
}
pythonProcess = spawn(pythonCommands[currentCommand], ['api.py'], {
cwd: __dirname,
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env, PYTHONIOENCODING: 'utf-8' }
});
pythonProcess.stdout.on('data', (data) => {
console.log(`Python API: ${data.toString().trim()}`);
});
pythonProcess.stderr.on('data', (data) => {
const error = data.toString().trim();
console.error(`Python API Error: ${error}`);
});
pythonProcess.on('error', (error) => {
console.error(`Failed to start Python with ${pythonCommands[currentCommand]}:`, error.message);
currentCommand++;
setTimeout(tryStartPython, 1000);
});
pythonProcess.on('close', (code) => {
console.log(`Python API process exited with code ${code}`);
if (code !== 0 && currentCommand < pythonCommands.length - 1) {
currentCommand++;
setTimeout(tryStartPython, 1000);
}
});
}
tryStartPython();
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html');
// Start Python API after window is ready
setTimeout(startPythonAPI, 2000);
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
// Kill Python process when closing
if (pythonProcess) {
pythonProcess.kill('SIGTERM');
}
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Cleanup on app quit
app.on('before-quit', () => {
if (pythonProcess) {
pythonProcess.kill('SIGTERM');
}
});