简体   繁体   English

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

[英]Sending data from Python to Javascript using EEL

I am trying to send data from python to Javascript using EEL and their documentation and it does not seem to work... I keep getting null in my html / js page.我正在尝试使用 EEL 和他们的文档将数据从 python 发送到 Javascript,但它似乎不起作用......我的 html/js 页面一直为空。

Here is what I have.这是我所拥有的。 Basically I want to get the link of the BING wallpaper and use it in my page as a background.基本上我想获取 BING 壁纸的链接并将其用作我的页面中的背景。 But until then, I want to first get the result.但在那之前,我想先得到结果。

BING py script: 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()

The script works and it returns my URL in the Python console.该脚本有效并在 Python 控制台中返回我的 URL。

My main.py which has EEL is as following:我有 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...!')

I have used an async command just as in their examples, like so:我在他们的示例中使用了 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>

Please help me understand how all this works.请帮助我了解这一切是如何运作的。

Not sure if you accidentally formatted your code wrong, but it's a little off.不确定您是否不小心将代码格式错误,但这有点不对。 Also you imported bs4 and json when you don't need to.当您不需要时,您还导入了 bs4 和 json。

Your scrape_bing() function was not returning anything.您的 scrape_bing() 函数没有返回任何内容。 It needs to return a value to " myDef " when assigning it in " myDef = scrape_bing() ".在“ myDef = scrape_bing() ”中赋值时,它需要将一个值返回给“ myDef ”。

I changed yours up a bit and came up with this example that will hopefully get you started.我稍微改变了你的,并提出了这个例子,希望能让你开始。 Hope this helps.希望这可以帮助。


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网页\\我的脚本.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网页\\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>

Also thanks for introducing me to eel.也谢谢你给我介绍鳗鱼。 First time using it and really like it :)第一次使用它,真的很喜欢它:)

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

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