简体   繁体   中英

Array not passing from Python Eel to Javascript

Hi I'm currently working on a desktop app using Python Eel, JS and HTML, but cannot seem to get a Python List to pass into JS for a new function. The Python side is working fine as it prints out the list of expected file names for me. If I insert an let array = ['a','b','c'] into the JS script then I get 3 buttons labelled as so, however when trying to pass the list from Python to JS it doesn't work and I am getting a callback=null error. I've tried multiple ways of fixing this but can't seem to find the answer: (PS. I have tried with the json lines both commented and uncommented and neither of these worked).

Python Script:

@eel.expose
def OverView_Data():
    Acc_Files = os.listdir("C:\\Users\\Bex\\Records\\Accessioning")
    Acc_Files = [x[:-4] for x in Acc_Files]
    #Acc_Files = json.dumps(Acc_Files)
    #Acc_Files = json.loads(Acc_Files)
    print(Acc_Files)
    return (Acc_Files)

JS Script:

eel.expose(OverView);
async function OverView(){
    let Array = await eel.OverView_Data()
    console.log(Array)
    for (var i=0; i<Array.length; i++) {
    var btn = document.createElement("button");
    var t = document.createTextNode(Array[i]);
    btn.appendChild(t);
    document.body.appendChild(btn);
    }
}

Try using this line instead:

let Array = await eel.OverView_Data()();

The difference is that () on the end of your existing line. You can read more about why this approach is needed on the Eel documentation page (just search page for await for the relevant section).

Lastly, an unrelated suggestion... you should consider avoiding the use of a JavaScript variable named Array since Array is a Global object in JavaScript and it could cause some problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM