使用EEL将数据从Python发送到Javascript - javascript

我正在尝试使用EEL及其文档将数据从python发送到Javascript,但它似乎不起作用...我的html / js页面中一直保持null。

这就是我所拥有的。基本上,我想获取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”。

我稍微改变了一下,提出了这个示例,希望可以帮助您入门。希望这可以帮助。

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>

也感谢您向我介绍鳗鱼。第一次使用它,真的很喜欢它:)

执行onclick时获得意外令牌 - javascript

我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…

Javascript到PHP的转换 - javascript

我有一个libphonenumber软件包的javascript端口,它具有以下功能:function cleanPhone(a){ a=a.replace(/[^\d\+]/g,""); return a="+"==a.substr(0,1)?"+"+a.replace(/[^\d]/g,…

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…