python-eel / Eel

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

Setting block=False at eel.start() gives error #552

Open Lioncat2002 opened 2 years ago

Lioncat2002 commented 2 years ago

Eel version 0.14.0

Describe the bug When the app is started with block=False, It gives the error as Error site can't be reached Issue similar to this one: #167

To Reproduce Steps to reproduce the behavior: try to start the app with eel.start("index.html",block=True) As show in the code snippet below:

import pyautogui
import eel
import datetime
import time
eel.init('front-end')
#--------------------------------------------------------------------------
#Problematic line
eel.start('index.html',size=(500,500),block=False) 
#this works fine as long as it is started without the block=False
#--------------------------------------------------------------------------
@eel.expose
def handshake(secs):
    secs=float(secs)
    while secs:
        eel.sleep(1)
        pyautogui.press('f15')

        print(f"Time: {datetime.timedelta(seconds=float(secs))}")
        return f"Time: {datetime.timedelta(seconds=float(secs))}"

Expected behavior The app should work normally but it gives LocalHost error

Screenshots image

Desktop (please complete the following information):

thatfloflo commented 2 years ago

I suspect your script might just finish and quit before you ever get around to connecting. Try something like this:

import eel
import gevent
eel.start('index.html',size=(500,500),block=False)
gevent.get_hub().join()

If you don't have eel block then you need to make sure the event loop is entered yourself as far as I can tell. This also leaves it up to you to quit the event loop when the last socket is closed (or whatever conditions/events happen that should produce that behaviour).

bernardolansing commented 2 years ago

Hello @thatfloflo. Thank you for sharing your solution. For me, it worked but only for the first time I ran my code. After that, localhost would be refused again. But you gave me the ideia to use eel.sleep(1.0) just after eel.start being called. I am not sure about the 1.0, but this way it started to work, at least for me. Worth trying.

Update: nah, this just makes it load the html, but JS is still not loaded, no matter what time interval I choose to eel.sleep. Does anyone have any tip? I really need to get this working soon...

thatfloflo commented 2 years ago

@bernardolansing I think it might be useful if you could share a minimal working example (with html files etc, maybe in a separate repo?) - there could be many potential issues causing this behaviour. That it loads the HTML and then quits suggests that it does briefly run the greenlet that serves it, but it's difficult to say more than that from the information we have here.

bernardolansing commented 2 years ago

@thatfloflo Actually I found out that there was an error in my Javascript file causing this behaviour. After commenting the entire file, everything started to work again. Thanks for your reply!

jon-vy commented 2 years ago

Guys thanks for helping out a lot

FreugdFred commented 2 years ago

Eel version 0.14.0

Describe the bug When the app is started with block=False, It gives the error as Error site can't be reached Issue similar to this one: #167

To Reproduce Steps to reproduce the behavior: try to start the app with eel.start("index.html",block=True) As show in the code snippet below:

import pyautogui
import eel
import datetime
import time
eel.init('front-end')
#--------------------------------------------------------------------------
#Problematic line
eel.start('index.html',size=(500,500),Block=False) 
#this works fine as long as it is started without the block=False
#--------------------------------------------------------------------------
@eel.expose
def handshake(secs):
    secs=float(secs)
    while secs:
        eel.sleep(1)
        pyautogui.press('f15')

        print(f"Time: {datetime.timedelta(seconds=float(secs))}")
        return f"Time: {datetime.timedelta(seconds=float(secs))}"

Expected behavior The app should work normally but it gives LocalHost error

Screenshots image

Desktop (please complete the following information):

  • OS: windows 11
  • Browser Google Chrome

Its sounds a little stupid but I had the same problem, until I discovered that block is with a cappitle capital B. Try Block=False :)

RARM commented 1 year ago

I was struggling with this a moment ago. At first, applying the changes suggested by @FreugdFred worked, but that is because there is no parameter Block. This change causes the parameter block to take the default value (True), which removes the ability to work asynchronously (but fixes the Error site can't be reached issue).

In my case, using block=False would cause the same Error site can't be reached unless the Python file meets some requirements. For example, it wouldn't work unless I made at least one call to a function exported by the JS (after eel.start). I haven't been able to test it, but it also seems that exposing a Python function after eel.start won't make it accessible to my front-end (but, again, I couldn't test this). I think it makes sense, but it is not in the documentation (README.md file).

It seems that the Eel library makes some assumptions on how the Python backend is structured. But, it is just my experience. I hope it helps somehow.

thatfloflo commented 1 year ago

@RARM the behaviour with the first call is probably because the gevent main loop needs to be initialised, otherwise the Python script will just finish executing and the process will terminate. You can manually import gevent, then get the current ghub and call .join() on the ghub to force this even if no call to eel is made via the websocket. If you do this, remember to exit the gevent main loop manually (e.g. with gevent.kill(gevent.get_hub()).