[英]Sending data from Python to Javascript using EEL
我正在尝试使用 EEL 和他们的文档将数据从 python 发送到 Javascript,但它似乎不起作用......我的 html/js 页面一直为空。
这是我所拥有的。 基本上我想获取 BING 壁纸的链接并将其用作我的页面中的背景。 但在那之前,我想先得到结果。
BING py 脚本:
import bs4
import requests
import json
def scrape_bing():
BASE_PATH = 'http://www.bing.com'
BASE_REST = '/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'
URL = BASE_PATH + BASE_REST
r = requests.get(url=URL)
if r.status_code == 200:
data = r.json()
wallpaper_path = BASE_PATH + data['images'][0]['url']
print(wallpaper_path)
else:
raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(URL))
def main():
scrape_bing()
if __name__ == '__main__':
main()
该脚本有效并在 Python 控制台中返回我的 URL。
我有 EEL 的 main.py 如下:
import eel
from inc.bing import scrape_bing
eel.init('web')
myDef = scrape_bing()
@eel.expose
def bingR():
return myDef
try:
eel.start('index.html', mode='chrome', host='localhost', port=8274)
except (SystemExit, MemoryError, KeyboardInterrupt):
pass
print ('Closed browser log...!')
我在他们的示例中使用了 async 命令,如下所示:
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
async function run() {
let n = await eel.bingR()();
console.log('Got this from Python: ' + n);
}
run();
</script>
请帮助我了解这一切是如何运作的。
不确定您是否不小心将代码格式错误,但这有点不对。 当您不需要时,您还导入了 bs4 和 json。
您的 scrape_bing() 函数没有返回任何内容。 在“ myDef = scrape_bing() ”中赋值时,它需要将一个值返回给“ myDef ”。
我稍微改变了你的,并提出了这个例子,希望能让你开始。 希望这可以帮助。
主文件
import eel
import requests
eel.init('web')
@eel.expose
def bingR():
BASE_PATH = 'http://www.bing.com'
BASE_REST = '/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'
URL = BASE_PATH + BASE_REST
r = requests.get(url=URL)
if r.status_code == 200:
data = r.json()
wallpaper_path = BASE_PATH + data['images'][0]['url']
print(wallpaper_path)
return wallpaper_path
return 'No wallpaper found'
try:
eel.start('index.html', mode='chrome', host='localhost', port=8274)
except (SystemExit, MemoryError, KeyboardInterrupt):
pass
print ('Closed browser log...!')
网页\\我的脚本.js
async function run() {
let n = await eel.bingR()();
console.log('Got this from Python: ' + n);
document.getElementById('output').value = n;
}
run();
网页\\index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript" src="/myscript.js"></script>
<input id="output" value="Output here" style="width: 700px;">
</body>
</html>
也谢谢你给我介绍鳗鱼。 第一次使用它,真的很喜欢它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.