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

Returning intermediate result during function call #295

Closed datainvestor closed 4 years ago

datainvestor commented 4 years ago

I'm trying to figure out how or maybe if it is possible to return some sort of information when python function is being called and is running some loop that has variable which is incremented. Lets say I have a function that saves a file to a excel file:

@eel.expose
def xlsx_converter(path, sheet, output):
        start = time.process_time()
        wb =  openpyxl.load_workbook("input/" + path, read_only=True) 
        ws = wb[sheet]
        row_count = ws.max_row
        with open("input/"+output, "w") as out:
            writer = csv.writer(out)
            counter = 0
            for row in ws:
                if counter/row_count == 0.5:
                        #return "Intermediate result message-info"
                elif counter/row_count == 0.75: 
                       #return "Another Intermediate result message-info"
                else:
                        continue
                values = (cell.value for cell in row)
                writer.writerow(values)
                counter+=1

So given some condition (when the counter variable reaches some value) I would like to send that msg to my frontend. Is this possibility? How would I retrieve this in frontend?

NecroticOoze commented 4 years ago

In the front end you have to add an exposed JS function that does what you want. Like:

eel.expose(myFunc)
function myFunc() { //Do something}

In your backend, call that function when

if counter == 99: #or whatever value
     eel.myFunc()
else:
     pass

Let me know if you still need help :)

datainvestor commented 4 years ago

@NecroticOoze Thanks for the response. Acutally I'm still not sure how this could be applied? Tbh I'm using VueJS as my frontend library so this makes it extra difficult I guess. From what I understand is that I sholud define a function in frontend and call it in backend and then the result is calculated in frontend right? So its the opposite of decalring function in backend to use it in frontend right? (@eel.expose in python app)

NecroticOoze commented 4 years ago

@datainvestor

I'm not entirely sure how VueJS works, but it does sound like you understand just the core concepts of what I was trying to explain. I'm not too sure that I can help you any further, the only thing I can say is that experimentation is the key to understanding.

RahulBadenkal commented 4 years ago

@datainvestor The javascript functions you expose from eel must be availble from window. (i.e you should be able to call it from chrome console directly). Since you are using vue and if the function you are trying to expose (as per @NecroticOoze suggestion) is aslo inside vue scope, then it won't be callable from window directly (and thus python can't call it).

I had a similar issue when using it with angularjs. What you can do is something like this:

Now as per @NecroticOoze call outerFunction() from python which will indirectly call the function you want inside your vue app.

A sample I coded for angularjs:

function updateProgressBarMsg(msg) { let ctrlScope = angular.element(document.getElementsByTagName('body')[0]).scope(); ctrlScope.progressBarMsg = msg; }

My angular app has a variable called progressBarMsg whose value I wanted to change via python. So I wrote a function updateProgressBarMsg function outside of my angular app and got the angular app's scope inside it. (The angular app was defined on the body of html page). Then via that i accessed the updateProgressBarMsg variable.

samuelhwilliams commented 4 years ago

I'm going to close this due to inactivity - hopefully you've worked it out now from the help provided :)