0

I want to call a function using eel that isn't available before the program runs.

With plain JS it works just fine, I need some workaround or something similar for TS.

Python file

import eel

eel.init('web', allowed_extensions=['.js', '.html'])

@eel.expose
def my_python_function():
    print(2)

eel.start('index.html', mode='chrome', cmdline_args=['--kiosk'])

Html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="myButton"></button>

    <script type="text/javascript" src="/eel.js"></script>
    <script type="module" src="js/index.js"></script>
</body>
</html>

Working JS

let button = document.getElementById("myButton");

button.onclick = buttonClicked;

function buttonClicked()
{
    console.log("Clicked");
    eel.my_python_function();
}

If I am not using TS in it's intended way I'm sorry, I'm a beginner in web dev.

The following code is what i tried in TS but didn't work

let button : HTMLElement | null= document.getElementById('myButton');

button?.addEventListener("click", buttonClicked)

function buttonClicked()
{
    console.log("Clicked");
    eel.my_python_function(); // Error here
}

Rafael
  • 11
  • 3
  • If i could ignore the error `Cannot find name 'eel'.ts(2304)` it would work i think – Rafael Jan 23 '23 at 23:36
  • Since you say you're a beginner, I will ask you a rhetorical question: why does your typescript look different from your JavaScript besides having type annotations? – Aluan Haddad Jan 24 '23 at 01:22

1 Answers1

-1

I got it working by ignoring the error.

let button : HTMLElement | null= document.getElementById('myButton');

button?.addEventListener("click", buttonClicked)

function buttonClicked()
{
    console.log("Clicked");
    
    // @ts-ignore
    eel.my_python_function();
}
Rafael
  • 11
  • 3
  • This is not an answer. It suggests there's no way to solve the problem but it seems like you didn't explain why. In fact the problem can be solved – Aluan Haddad Jan 24 '23 at 01:19
  • @AluanHaddad well, do you mind explaining for the rest of us? I believe that's a valid answer since I asked for a fix or workaround, but yes I agree there are better answers and I'd be glad to know. – Rafael Jan 24 '23 at 11:44