0

I have made a Python program which has an Eel frontend. The backend of this program is used for operating a robot. The robot is picking up objects from a tray, put it into a lasermachine, waits for lasermachine and put the object back to tray and so on.

After I started this loop from my Eel frontend with a start button I want to give the user the ability to stop the program aswell. In one tray there are 100 objects. So after each object I can of course stop the program with a simple check in a while loop. But that is not what I want. When the stop button is pressed the process should stop immediately. My idea was to work with try-catch and raise an expection but so far without any luck. The program will not stop or I get an Eel error message.

What would be the right way to immediately stop the will loop and stop the robot moving.

main.html

<!DOCTYPE html>
<html>
    <head>
        <title>Hello</title>
        <link rel="stylesheet" href="css/style.css">

        <script src="js/jquery-3.6.3.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="/eel.js"></script>
        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body>
        <button type="button" id="startBtn" style="--bs-btn-padding-y: 0.75rem; --bs-btn-padding-x: 1.5rem;" class="btn btn-success">Start</button>
        <button type="button" id="stopBtn" style="--bs-btn-padding-y: 0.75rem; --bs-btn-padding-x: 1.5rem;" class="btn btn-success">Stop</button>
    </body>
</html>

script.js

$(function() {

    $("#startBtn").click(function() {
        eel.startprogram();
    });

    $("#stopBtn").click(function() {
        eel.stopprogram();
    });
});

web.py

import gevent.monkey
gevent.monkey.patch_all()
import eel
import time

# Set web files folder
eel.init('web')


@eel.expose
def startprogram():
    try:
        while True:
            t = time.localtime()
            current_time = time.strftime("%H:%M:%S", t)
            print(current_time)
            print("running program for 1 minute")
            # in the while loop the robot is picking up an object, bring it to the lasermachine, put it back to the tray. 
            time.sleep(60)

    except Exception:
        print("Hello exception")


@eel.expose
def stopprogram():
    print("Stop clicked")
    raise Exception


eel.start('main.html', size=(800, 600), block=True)  # Start
michel3vb
  • 71
  • 1
  • 7

0 Answers0