简体   繁体   English

无法通过鳗鱼从python访问javascript function

[英]Can't access javascript function from python through eel

I've been trying to learn the basics of Eel through their documentation.我一直在尝试通过他们的文档学习 Eel 的基础知识。 I struggled learning how to trigger a javascript function through python so I tried to download their Hello World example straight from their github. You can open it here.我努力学习如何通过 python 触发 javascript function,所以我尝试直接从他们的 github 下载他们的 Hello World 示例。您可以在此处打开它。

But I still get the same error, namely: Exception has occurred: AttributeError module 'eel' has no attribute 'say_hello_js'但我仍然得到同样的错误,即:发生异常:AttributeError 模块 'eel' 没有属性 'say_hello_js'

The code is as following: (you can also look it up on github in the link abov)代码如下:(也可以在上面的链接github上查)

hello.py你好.py

    import eel
    
    # Set web files folder
    eel.init('web')
    
    @eel.expose                         # Expose this function to Javascript
    def say_hello_py(x):
        print('Hello from %s' % x)
    
    say_hello_py('Python World!')
    eel.say_hello_js('Python World!')   # Call a Javascript function
    
    eel.start('hello.html', size=(300, 200))  # Start

web/index.html网站/index.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>Hello, World!</title>
    
        <!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
        <script type="text/javascript" src="/eel.js"></script>
        <script type="text/javascript">
          eel.expose(say_hello_js); // Expose this function to Python
          function say_hello_js(x) {
            console.log("Hello from " + x);
          }
    
          say_hello_js("Javascript World!");
          eel.say_hello_py("Javascript World!"); // Call a Python function
        </script>
      </head>
    
      <body>
        Hello, World!
      </body>
    </html>

Could it be that I have a wrong version of python or Eel?会不会是我python或者Eel的版本错误? I am running Python 3.8.10.我正在运行 Python 3.8.10。 I ran pip install eel --upgrade but the issue still occurs.我运行了pip install eel --upgrade但问题仍然存在。

I cannot pin point the exact issue based on the details you provided.根据您提供的详细信息,我无法确定确切的问题。 But you can try the following these steps if you're using VSC:但如果您使用的是 VSC,则可以尝试执行以下这些步骤:

  1. Run this command py -m venv.venv and it will create your virtual environment.运行此命令py -m venv.venv它将创建您的虚拟环境。
  2. Select the virtual environment's Python interpreter with CTRL + SHIFT + K . Select 使用CTRL + SHIFT + K的虚拟环境的 Python 解释器。
  3. Close out your terminal with the trash icon.使用垃圾桶图标关闭您的终端。
  4. Start the terminal using the virtual environment's Python interpreter with CTRL + J .通过CTRL + J使用虚拟环境的 Python 解释器启动终端。
  5. Run the command pip install eel .运行命令pip install eel
  6. Create the file hello.py .创建文件hello.py
  7. Create the directory web .创建目录web
  8. In web create the file hello.html .web创建文件hello.html

In hello.py use this code:hello.py使用此代码:

import eel
eel.init('web')
eel.say_hello_js('Hi from Python')
eel.start('hello.html')

In hello.html use this code:hello.html使用此代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello, World!</title>

    <!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
    <script type="text/javascript" src="/eel.js"></script>
    <script type="text/javascript">
      eel.expose(say_hello_js); // Expose this function to Python
      function say_hello_js(x) {
        alert("Hello from " + x);
      }
    </script>
  </head>

  <body>
    Hello, World!
  </body>
</html>

If you followed these steps, you should see the alert box pop up.如果您按照这些步骤操作,您应该会看到弹出的警告框。 To trigger a JS function from Python you need to precede the function with the eel expose function like this eel.expose(your_js_function_name);要从 Python 触发 JS function,您需要在 function 之前使用 eel expose function 像这样eel.expose(your_js_function_name); . . From Python you would call this function using the eel module and calling that function name like this eel.your_js_function_name(parameter_if_any) .从 Python 你可以使用 eel 模块调用这个 function 并像这样调用 function 名称eel.your_js_function_name(parameter_if_any) Make sure that in your HTML file you have the eel script element in the head.确保在您的 HTML 文件的头部有 eel 脚本元素。 Then another script element after that one with all your functions.然后是另一个具有所有功能的脚本元素。

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

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