简体   繁体   English

数组未从 Python Eel 传递到 Javascript

[英]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.嗨,我目前正在使用 Python Eel、JS 和 HTML 开发桌面应用程序,但似乎无法将 Python 列表传递给 JS 以获取新的 function。Python 端工作正常,因为它打印出预期文件列表给我的名字。 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.如果我将 let array = ['a','b','c'] 插入 JS 脚本中,那么我会得到 3 个标记为这样的按钮,但是当尝试将列表从 Python 传递给 JS 时,它不起作用并且我收到一个 callback=null 错误。 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).我已经尝试了多种方法来解决这个问题,但似乎找不到答案:(PS。我已经尝试使用 json 行进行评论和取消评论,但这些都不起作用)。

Python Script: Python 脚本:

@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).您可以在Eel 文档页面上阅读更多关于为什么需要这种方法的信息(只需搜索相关部分的await页面)。

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.最后,一个不相关的建议......你应该考虑避免使用名为Array的 JavaScript 变量,因为Array是 JavaScript 中的全局变量 object,它可能会导致一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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