python-eel / Eel

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

error when using block=False on eel.start() #336

Closed marticliment closed 4 years ago

marticliment commented 4 years ago

I was trying to use block=False on eel.start() and mode=None to start the server without any GUI anbd then, with cefpython, open a new GUI. When i tried this, CEFpython appeared with a white screen, and then I tried mode="chrome", and chrome displayed error saying that localhost:4567 was not accesible, but if I set block=True it was working perfectly.

Python3 64bit Windows 10 Pro 64bit Cefpython v66 chrome 83

Code:

import eel
eel.init('web')
eel.start(mode=chrome, port=4567, block=True) #Working Fine
eel.start(mode=chrome, port=4567, block=False) #Page not found Error like this:

image

<html>
   <!-- some html text-->
</html>

Thanks :)

krpatter-intc commented 4 years ago

Make sure you follow it up with the while loop + sleep as mentioned in the docs.

marticliment commented 4 years ago

But cefpython also does a loop when you call cef.start()... Because if I put the loop before cef, I can't call it, and if I put it after, it does not run until I close cef window...

El dg., 28 de juny 2020, 18:55, Kevinrp01 notifications@github.com va escriure:

Make sure you follow it up with the while loop + sleep as mentioned in the docs.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/samuelhwilliams/Eel/issues/336#issuecomment-650793160, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVIW27JP6556VSADUYPLBTRY5YWXANCNFSM4OKTJZ5Q .

samuelhwilliams commented 4 years ago

With block=False, you need to have other code that keeps the Python process running - otherwise it will reach the end of the program and terminate, which closes the Eel server and results in the error you're seeing.

@Kevinrp01 is right - you can use an Eel sleep loop to keep the server alive, which seems to be what you want. https://github.com/samuelhwilliams/Eel#asynchronous-python

marticliment commented 4 years ago

Not working... I'm trying this:


import eel
eel.init('web')

def cef_thread():
    from cefpython3 import cefpython as cef
    import sys
    eel.sleep(5)
    sys.excepthook = cef.ExceptHook
    cef.Initialize()
    cef.CreateBrowserSync(url="localhost:4567")
    cef.MessageLoop()
    cef.Shutdown()
    while True:
        print("I'm a thread")
        eel.sleep(1.0)                  

eel.spawn(cef_thread)

eel.start('index.html',mode='chrome', size=(900, 500), port=4567,  block=False)  

while True:
    print("I'm a main loop")
    eel.sleep(1.0)   

So, It starts running nice for the 5 first seconds (ot for the x seconds that are set on line 7) and then, while cefpython is running, the main loop stops printing "I'm a main loop" and if I reload the chrome window, it gives me the error. Then, when I close the cefpython window, the program starts printing "I'm a thread" as normal, because the cef_thread() function ends with a final loop, and the "I'm a main loop" loop recovers activity, and, if I reolad chrome window, it reloads perfectly the localhost:4567 page. So I think the solution may be to run the cef_thread() on another process, instead of running it with eel.spawn(). Because the server and cef are running on the same process, and cef has a cef.MessageLoop that stills running until cef is colsed. I'll try it out and post the results...

marticliment commented 4 years ago

Well, Finally I managed to make it work. I used Process from multiprocessing instead of eel.spawn, because I needed two REAL different processes, not an emulation, because eel.spawn() uses a thread. Now it works, and the full code is that one:


@eel.expose
def checkUpdates_py():
    actualVersion = 2.4
    if True:
        import struct
        import urllib.request
        response = urllib.request.urlopen("http://www.somepythonthings.tk/versions/calc.html")
        response = response.read().decode("utf8")
        if float(response)>actualVersion:
            return True
        else:
            return False
    else:
        return False
@eel.expose
def downloadUpdates():
    import webbrowser
    webbrowser.open_new('https://www.somepythonthings.tk/programs/somepythonthings-calc/')

def eval2(s):
    try:
        return str(eval(str(s)))
    except:
        return 'Oh 💩, You did it again! The operation is too hard to be calculated!'
@eel.expose
def py_eval(s):
    return eval2(s)
@eel.expose()
def python_alive():
    return True

def server_process():
    print('Starting Eel')
    eel.start('index.html',mode=None, size=(900, 500), port=4567,  block=False)
    while True:
        print("Eel Server Running")
        eel.sleep(1.0)   

#This eel.init('web') crashes if it is on the "if __name__ == '__main__':"
eel.init('web')

if __name__ == '__main__':

    from cefpython3 import cefpython as cef
    from multiprocessing import Process
    import sys
    p = Process(target=server_process)
    p.start()
    sys.excepthook = cef.ExceptHook
    cef.Initialize()
    cef.CreateBrowserSync(url="localhost:4567/index.html")
    cef.MessageLoop()
    cef.Shutdown()

The thing is that now i can have the cef.MessageLoop() and eel.start() with its while True: working at the same time.

(If you try to run this, do NOT try it on IDLE, because it won't work on idle, it needs to be executed as a .py file.)

Lots of thanks for the help :D. I'll close this, because the problem is now solved.