python-eel / Eel

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

Eel throwing error when used with electron #636

Open 5hayanB opened 1 year ago

5hayanB commented 1 year ago

I'm trying to make a hello_world example Eel app. I have the following files.

run.py

import eel

@eel.expose
def hello_to_javascript():
    print('Hello Javascript!')

if __name__ == '__main__':
    eel.init('web')
    eel.browsers.set_path('electron', 'node_modules/electron/dist/electron')

    eel.hello_to_python()

    eel.start(
        'index.html',
        mode='electron'
    )

index.js

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

let win;

function createWindow() {
    win = new BrowserWindow({
        width: 1200,
        height: 600,
        resizable: false,
        webPreferences: {
            nodeIntegration: true
        }
    });

    win.loadURL('http:localhost:8000/index.html');

    win.on('closed', function () {
        win = null;
    });
}

app.whenReady().then(function () {
    createWindow();
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', function () {
    if (win === null) {
        createWindow();
    }
});

web/index.html

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="/eel"></script>
        <script type="text/javascript" src="js/test.js"></script>
    </head>
    <body>
        <h1>Hello Eel!</h1>
        <button onclick="eel.hello_to_javascript();">Greet back!</button>
    </body>
</html>

web/js/test.js

eel.expose(hello_to_python);
function hello_to_python() {
    console.log('Hello python');
}

When I do

python run.py

a window appears with the html showing in it. But when I take a look in the terminal, I find this error.

Traceback (most recent call last):
  File "/home/5hayanB/Repositories/temp-repositories/eel/venv/lib/python3.10/site-packages/bottle.py", line 876, in _handle
    return route.call(**args)
  File "/home/5hayanB/Repositories/temp-repositories/eel/venv/lib/python3.10/site-packages/bottle.py", line 1756, in wrapper
    rv = callback(*a, **ka)
  File "/home/5hayanB/Repositories/temp-repositories/eel/venv/lib/python3.10/site-packages/bottle_websocket/plugin.py", line 5, in wrapper
    callback(request.environ.get('wsgi.websocket'), *args, **kwargs)
  File "/home/5hayanB/Repositories/temp-repositories/eel/venv/lib/python3.10/site-packages/eel/__init__.py", line 249, in _websocket
    msg = ws.receive()
AttributeError: 'NoneType' object has no attribute 'receive'

I'm new to electron so it might be that I'm doing something obviously wrong here. But I followed the Eelectron example given in the examples folder. I tried running that example and it runs without any problem. I can't seem to figure out what I'm doing wrong here.