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

Using Eel from Electron main process? #659

Open triopter opened 1 year ago

triopter commented 1 year ago

Describe the problem I'd like to be able to communicate between Python and the Electron main process (in addition to between Python and the Electron renderer process). I've got it working just fine with the renderer process, but can't find a way to import Eel to the main electron process. I tried introducing delays to avoid race conditions, but to no avail.

Error (in Electron alert before startup):

Error: Cannot find module 'http://localhost:8080/eel.js'

Code snippet(s)

import eel

eel.init("src")

@eel.expose
def say_hello_py(_from):
    print(f"hello from {_from}!")

say_hello_py("Python world")
eel.say_hello_js("Python calling to JS")

electron_path = "node_modules/.bin/electron"

eel.start(
    {"port": 3000},
    mode="custom",
    host="localhost",
    port=8080,
    cmdline_args=[electron_path, "."],
)```

```javascript

const {app, BrowserWindow, dialog } = require('electron')

let eel
let appInstance

// @TODO: load env from env
let DEBUG = true
let WAIT = 0

setTimeout(() => { configure(); run(); }, 3 * 1000)

class App {
  // window setup here
  // want to call to Python
}

function configure() {
    require('http://localhost:8080/eel.js')
}

function run() {
    appInstance = new App()
}