Skip to content

syed-reza98/NetCon_PyVue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetCon: A microservices-based Reconciliation System

Extraction Service ( Python-Flask + Vue-Quasar )

This repository houses the source code and related documentation for NetCon: Extraction.

Packaging a Python script with Quasar Framework's Electron Mode involves integrating the Quasar Framework (a Vue.js-based frontend framework) with Electron to bundle the Python script as part of the Electron build. Here's how you can achieve it:


1. Set Up Your Python Script

  • Write your Python script and ensure it functions as expected.
  • If your Python script has dependencies, create a virtual environment:
    python -m venv venv
    source venv/bin/activate # On Windows: venv\Scripts\activate
    pip install -r requirements.txt
  • Use PyInstaller to convert the Python script into an executable:
    pip install pyinstaller
    pyinstaller --onefile your_script.py
    This will create a standalone executable (e.g., your_script.exe on Windows) in the dist folder.

2. Set Up the Quasar Project

  • Install the Quasar CLI:

    npm install -g @quasar/cli
  • Create a new Quasar project:

    quasar create my-quasar-electron-app

    Follow the prompts to configure your project.

  • Navigate to your project directory:

    cd my-quasar-electron-app
  • Add Electron Mode to your project:

    quasar mode add electron

3. Integrate the Python Executable with Electron

  • Place the Python executable (generated by PyInstaller) into a folder within your project, such as src-electron/python.

  • Modify the Electron main process file (src-electron/electron-main.js) to run the Python executable using Node.js's child_process module:

    const { app, BrowserWindow } = require('electron');
    const { spawn } = require('child_process');
    
    let mainWindow;
    
    app.on('ready', () => {
      mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
          nodeIntegration: true,
          contextIsolation: false, // Required for Quasar apps
        },
      });
    
      mainWindow.loadURL('http://localhost:8080'); // This loads the Quasar app
    
      // Run the Python executable
      const pythonProcess = spawn('src-electron/python/your_script.exe'); // Adjust path as needed
    
      pythonProcess.stdout.on('data', (data) => {
        console.log(`Python Output: ${data}`);
      });
    
      pythonProcess.stderr.on('data', (data) => {
        console.error(`Python Error: ${data}`);
      });
    
      pythonProcess.on('close', (code) => {
        console.log(`Python script exited with code ${code}`);
      });
    });

4. Build and Package the Application

  • Update the Electron build configuration in quasar.conf.js to include the Python executable in the final bundle:

    electron: {
      packager: {
        // Include the Python executable in the build
        extraResource: ['src-electron/python/your_script.exe'],
      },
      builder: {
        // For Electron Builder configuration (optional)
        extraFiles: [
          {
            from: 'src-electron/python',
            to: 'python',
          },
        ],
      },
    },
  • Build and package the Electron app:

    quasar build -m electron

    This will produce platform-specific builds (e.g., .exe for Windows, .dmg for macOS) in the dist/electron folder.


5. Test the Packaged Application

  • Test the packaged app on the target platform to verify the Python script runs correctly when invoked by the Electron main process.

Additional Notes

  • Ensure proper error handling for the Python executable in the Electron code.
  • If the Python script communicates with the frontend, consider using IPC (Inter-Process Communication) between Electron and your Vue.js-based Quasar frontend.

By following these steps, you can successfully package a Python script with Quasar Framework's Electron Mode.

To ensure the Python .exe file is terminated when the Electron app is closed, you can listen for the app.on('window-all-closed') and app.on('quit') events in the Electron main process and kill the Python process accordingly. Here's how you can do it:

Example Code for electron-main.js

const { app, BrowserWindow } = require('electron');
const { spawn } = require('child_process');

let mainWindow;
let pythonProcess;

function createMainWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false, // Required if you're using Quasar or similar frameworks
    },
  });

  mainWindow.loadURL('http://localhost:8080'); // Replace with your app's URL

  // Spawn the Python process
  pythonProcess = spawn('src-electron/python/your_script.exe'); // Adjust the path to your Python executable

  pythonProcess.stdout.on('data', (data) => {
    console.log(`Python Output: ${data}`);
  });

  pythonProcess.stderr.on('data', (data) => {
    console.error(`Python Error: ${data}`);
  });

  pythonProcess.on('close', (code) => {
    console.log(`Python script exited with code ${code}`);
  });

  // Handle window close
  mainWindow.on('closed', () => {
    mainWindow = null;
  });
}

// Kill the Python process when the app quits
app.on('quit', () => {
  if (pythonProcess) {
    pythonProcess.kill(); // Kill the Python process
  }
});

app.on('window-all-closed', () => {
  // On macOS, apps usually stay open unless explicitly quit
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // Re-create a window in the app when the dock icon is clicked (macOS)
  if (mainWindow === null) {
    createMainWindow();
  }
});

// Start the app
app.on('ready', createMainWindow);

Key Points

  1. Spawning the Python Process:

    • Use Node.js's child_process.spawn() to launch the Python executable.
    • Store the reference to the process in a variable (pythonProcess).
  2. Killing the Process:

    • Call pythonProcess.kill() during the app.on('quit') event to ensure the Python process is terminated when the Electron app exits.
  3. Window Close Behavior:

    • On macOS, the app typically stays open when all windows are closed. Add logic to explicitly quit the app when appropriate.
  4. Error Handling:

    • Listen for errors on pythonProcess.stderr to catch any issues with the Python executable.

By implementing this, the Python process will be terminated cleanly when the Electron app is closed.

About

No description, website, or topics provided.

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published