python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.44k stars 587 forks source link

How to add and call multiple functions from Python to JS #296

Closed LonaExe closed 4 years ago

LonaExe commented 4 years ago

Hey all, I'm trying to make a bunch of functions and call them from Python to Js. Even though I can see the output in the command line, I cannot alert() it or use innerHTML.. I have no idea if multiple functions are supported by Eel, and if they are, how to call them. I'm fairly good at Python but I don't know much JS. Its probably a silly mistake, but any help would be appreciated.

import eel
eel.init('web')
@eel.expose
def function_one():
  return "here is a function"
def function_two():
  return "I'm in love with Python"
eel.start('main.html')
...
<html>
<head>
        <title>Eel</title>
        <script type="text/javascript" src="/eel.js"></script>
        <script>
                async function getResult() {
                var question = document.getElementById('data').value
                var functionOne = await eel.function_one()();
                var functionTwo = await eel.function_two()();              
                alert(functionOne)
                alert(functionTwo) 
    }
        </script>
    </head>
    <body>
        <button onclick="getResult()">Click Me</button>
    </body>
</html>

Desktop (please complete the following information):

samuelhwilliams commented 4 years ago

It doesn't look like you've exposed the second function. You need to add the @eel.expose decorator before every function in Python that you want to expose to JavaScript.

Does that help/fix your issue?

LonaExe commented 4 years ago

Ohh.. Thanks a lot dude, I thought we needed to just add one. I'm so dumb. Thanks again

samuelhwilliams commented 4 years ago

No worries :)