python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.5k stars 592 forks source link

Add Electron + CreateReactApp Eexample #696

Closed daniel-carvajal closed 1 year ago

daniel-carvajal commented 1 year ago

Should close https://github.com/python-eel/Eel/issues/216, https://github.com/python-eel/Eel/issues/57, https://github.com/python-eel/Eel/issues/428 Relates to https://github.com/python-eel/Eel/issues/285, https://github.com/python-eel/Eel/issues/544, https://github.com/python-eel/Eel/issues/266, https://github.com/python-eel/Eel/issues/607, https://github.com/python-eel/Eel/issues/159, https://github.com/python-eel/Eel/issues/491

Also somewhat followed this article as reference to configure CRA app with Electron.

daniel-carvajal commented 1 year ago

Attempting to work out issue related to process not terminating properly on Electron window close, will reopen then.

daniel-carvajal commented 1 year ago

Python app terminates correctly now when Electron is closed, using:

in main.js

  mainWindow.on("close", function (e) {
    if (mainWindow) {
      mainWindow.webContents.send("app-close");
    }
  });

in App.tsx

   componentDidMount() {
    ipcRenderer.on('app-close', () => {
      eel.quit_app()
    });
   }

and in eel_electron_CRA.py

@eel.expose
def quit_app():
    """Quit the app."""
    print('Exiting python app') 
    os._exit(0)

---onefile --noconsole flags needs to be further tested though.

daniel-carvajal commented 1 year ago

Currently main.js uses this configuration (may need improvements) as discussed here:

webPreferences: {
  preload: path.join(__dirname, 'preload.js'),
  contextIsolation: false,
  nodeIntegration: true,
  nodeIntegrationInWorker: true
}