python-eel / Eel

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

Opening and closing a WebDriver from the EEL GUI. #602

Closed maximedrn closed 2 years ago

maximedrn commented 2 years ago

Hello, As the title says, I am trying to make a GUI with a simple button to start and close a WebDriver. When it is open, the WebdDriver performs some tasks for an indefinite time. When the user wants to stop the process, he just clicks on the button to close it and return to the GUI.

I've read a few issues and Stackoverflow pages on similar topics, but none of them helped me in this case.

Here is the code I use, I removed the part about multiprocessing/threading/pool because none of them work.

main.py

#!usr/bin/python

import eel

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as SC
from webdriver_manager.chrome import ChromeDriverManager as CDM

@eel.expose
def open():
    """Open the webdriver in a parallel process."""

def web():
    global driver
    driver = webdriver.Chrome(service=SC(CDM(log_level=0).install()))
    # Does its tasks.

@eel.expose
def close():
    """Stop the webdriver from the parallel process."""

if __name__ == '__main__':
    eel.init('.')
    eel.start('index.html')

index.html

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="/eel.js"></script>
        <script>
            let isRunning = false;
            function webdriver() {
                isRunning ? eel.close() : eel.open();
                isRunning = !isRunning;
            }
        </script>
    </head>

    <body>
        <button onclick="webdriver()">Webdriver</button>
    </body>
</html>

The files are in the same directory. I'm using Windows 11 with the latest version of the ChromeDriver and the latest version of the eel module on Python 3.9.9.

dardevelin commented 2 years ago

Disclaimer: I have no used the project extensively enough to be able to advise with confidence. All tips in here are just attempts at providing some pointers since I saw that the project is semi-abandoned as per this GitHub issue: https://github.com/ChrisKnott/Eel/issues/600

Assumptions

    • eel behaves somewhat a little like selenium in that the web driver is not a python process
    • eel has an event loop which keeps the application running
    • eel has interrupt mechanism for functions that are being called from javascript/browser side

First Idea Use psutil python module to kill the new instance/window and kill it when done. trap the exception in python code. not clean but might get you there.

Considerations: Based on point 1, it would mean that stopping a full window may not be possible without 'killing' the driver. If it's like selenium, doing so would destroy all instances of eel or the browser at once, making my first idea no viable.

Second Idea Once again assuming it's like selenium you might be able to create a new 'tab' in the browser, which you can also detach. The important part is that by doing so you would be retaining independent control of each "window" and effectively be able to easily kill it using regular eel controls.

How would you be able to monitor once detached I don't know, maybe via a sub-process...

I hope any of this helps. Happy Hacking

maximedrn commented 2 years ago

Hello, thank you for you answer :) I tried the first option and it works great. I used the terminate() method to stop the process and then I kill all the processes of the webdriver using psutil.