使用EEL将数据从Python发送到Javascript
我正在尝试使用EEL及其文档将数据从python发送到Javascript,但它似乎不起作用。。。我的html/js页面中不断出现空值 这是我的。基本上,我想获得BING壁纸的链接,并在我的页面中将其用作背景。但在那之前,我想先得到结果 BING py脚本:使用EEL将数据从Python发送到Javascript,javascript,python,eel,Javascript,Python,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 = '/HPImag
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
我的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...!')
我使用了一个异步命令,就像在他们的示例中一样,如下所示:
<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>
异步函数run(){
让n=等待鳗鱼。宾格();
log('从Python获得:'+n);
}
run();
请帮助我了解所有这些是如何工作的。不确定您是否意外地将代码格式化错误,但有点不对劲。此外,您还可以在不需要时导入bs4和json 您的scrape_bing()函数没有返回任何内容。在“myDef=scrape\u bing()”中赋值时,需要将值返回到“myDef” 我对你的做了一些修改,并提出了这个例子,希望能让你开始学习。希望这有帮助
main.py
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...!')
web\myscript.js
async function run() {
let n = await eel.bingR()();
console.log('Got this from Python: ' + n);
document.getElementById('output').value = n;
}
run();
web\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>
测验
也谢谢你给我介绍鳗鱼。第一次使用它并且非常喜欢:)谢谢你,斯坎卡斯特,一切都很顺利。我想将bing代码添加到一个单独的文件中,然后将其附加到主屏幕中,我想我把事情搞砸了。我想创建一个桌面应用程序,EEL似乎是一个不错的选择,因为它允许您使用HTML+CSS:)