我有以下针对Eel的Python代码
import eel, urllib.request
@eel.expose
def python_function():
response = urllib.request.urlopen('http://google.com').read()
return(response)
eel.init('web')
eel.start('index.html', port=0)
现在当我用JavaScript调用它时
async function jsFunction() {
let result = await eel.python_function()();
return result;
}
我总是得到null
。当我从Python调用print(python_function())
时,我得到了一个预期的HTML字符串
测试时,JavaScript在到达URL之前返回null。我认为Python阻止了代码。我需要重新实现一个区块吗?从python_function
返回字符串将得到jsFunction
中的实际字符串
试试这个:
不同之处在于
response.decode('latin1')
返回在Eel的Python层中,必须返回可根据Python's JSON encoder进行序列化的对象,并且
read()
返回的原始二进制字符串不可序列化看看我给this question的答案,它详细解释了Eel的Python和JavaScript层之间的通信需求
另外,您应该在Python代码中解决的另一个问题是
eel.init('web')
应该出现在any@eel.expose
之前。正如目前编写的,您可能会看到JavaScript控制台中显示了一个错误,即无法加载某些资源相关问题 更多 >
编程相关推荐