Eel: Returning intermediate result during function call

Created on 15 May 2020  路  5Comments  路  Source: ChrisKnott/Eel

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?

help wanted

Most helpful comment

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 :)

All 5 comments

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 :)

@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)

@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.

@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:

  • Write a function outside of Vue (available from window) an expose it to python. call it outerFunction()
  • Within this function get the Vue's scope. (Somehow get a reference to the vue app here).
  • Then call vue_reference.my_function_inside_vue.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattiaornaghi picture mattiaornaghi  路  5Comments

logxseven picture logxseven  路  6Comments

TheBdouilleur picture TheBdouilleur  路  5Comments

sameera-madushan picture sameera-madushan  路  3Comments

ghost picture ghost  路  6Comments