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

Not able to get return value from Python method to JS variable #502

Closed sooraj-sn closed 3 years ago

sooraj-sn commented 3 years ago

@eel.expose def GetPath(): return "C:/Adds"

function displaygraph() { var data = document.getElementById("counter_name").value document.getElementById("frame").src = eel.GetPath() }

the return value is some function and not the string expected:

Result : http://localhost:8000/function%28callback%20%3D%20null%29%20%7B%20%20%20%20%20%20%20%20%20%20%20%20if%28callback%20%21%3D%20null%29%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20eel._call_return_callbacks%5Bcall.call%5D%20%3D%20%7Bresolve%3A%20callback%7D%3B%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20new%20Promise%28function%28resolve%2C%20reject%29%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20eel._call_return_callbacks%5Bcall.call%5D%20%3D%20%7Bresolve%3A%20resolve%2C%20reject%3A%20reject%7D%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%29%3B%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%7D'

sooraj-sn commented 3 years ago

Please help in getting the correct value

jvanmelckebeke commented 3 years ago

the trick is to make your function displaygraph async, and await eel.GetPath()() (note the double parentheses).

So, your python code stays

@eel.expose
def GetPath():
    return "C:/Adds"

But your javascript code should be:

async function displaygraph() {
    var data = document.getElementById("counter_name").value;
    document.getElementById("frame").src = await eel.GetPath()();
}

See this example, or alternatively you can also include a callback like in this example