Skip to content

Commit

Permalink
Add button to open Terminal and pip list in python environment. (#130)
Browse files Browse the repository at this point in the history
* Add button to open Terminal and pip list in python environment.

* Refactor.
  • Loading branch information
robinjhuang authored Oct 26, 2024
1 parent 36ff3f7 commit 87cbd03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ if (!gotTheLock) {
});
await handleFirstTimeSetup();
const { appResourcesPath, pythonInstallPath, modelConfigPath, basePath } = await determineResourcesPaths();

port = await findAvailablePort(8000, 9999).catch((err) => {
log.error(`ERROR: Failed to find available port: ${err}`);
throw err;
});

sendProgressUpdate('Setting up Python Environment...');
const pythonEnvironment = new PythonEnvironment(pythonInstallPath, appResourcesPath, spawnPythonAsync);
await pythonEnvironment.setup();
SetupTray(
mainWindow,
basePath,
Expand All @@ -190,17 +199,9 @@ if (!gotTheLock) {
},
() => {
mainWindow.webContents.send(IPC_CHANNELS.TOGGLE_LOGS);
}
},
pythonEnvironment.pythonInterpreterPath
);
port = await findAvailablePort(8000, 9999).catch((err) => {
log.error(`ERROR: Failed to find available port: ${err}`);
throw err;
});

sendProgressUpdate('Setting up Python Environment...');
const pythonEnvironment = new PythonEnvironment(pythonInstallPath, appResourcesPath, spawnPythonAsync);
await pythonEnvironment.setup();

sendProgressUpdate('Starting Comfy Server...');
await launchPythonServer(pythonEnvironment.pythonInterpreterPath, appResourcesPath, modelConfigPath, basePath);
} catch (error) {
Expand Down
26 changes: 25 additions & 1 deletion src/tray.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Tray, Menu, BrowserWindow, app, shell } from 'electron';
import path from 'path';
import { IPC_CHANNELS } from './constants';
import { exec } from 'child_process';
import log from 'electron-log/main';
import { PythonEnvironment } from './pythonEnvironment';

export function SetupTray(
mainView: BrowserWindow,
basePath: string,
modelConfigPath: string,
reinstall: () => void,
toggleLogs: () => void
toggleLogs: () => void,
pythonEnvironment: PythonEnvironment
): Tray {
// Set icon for the tray
// I think there is a way to packaged the icon in so you don't need to reference resourcesPath
Expand Down Expand Up @@ -95,6 +99,26 @@ export function SetupTray(
label: 'Toggle Log Viewer',
click: () => toggleLogs(),
},
{
label: 'Install Python Packages (Open Terminal)',
click: () => {
// Open a Terminal locally and
const pythonDir = path.dirname(pythonEnvironment.pythonInterpreterPath);
const pythonExe = path.basename(pythonEnvironment.pythonInterpreterPath);
const command =
process.platform === 'win32'
? `start powershell.exe -noexit -command "cd '${pythonDir}'; .\\${pythonExe} -m pip list"`
: `osascript -e 'tell application "Terminal"
do script "cd \\"${pythonDir}\\" && ./${pythonExe} -m pip list"
activate
end tell'`;
exec(command, (error, stdout, stderr) => {
if (error) {
log.error(`Error executing command: ${error}`);
}
});
},
},
]);

tray.setContextMenu(contextMenu);
Expand Down

0 comments on commit 87cbd03

Please sign in to comment.