Utilize TypeScript to interact with eel objects

I need to execute a function using eel that is not available until the program is running.

With vanilla JS, this works without any issues. However, I am looking for a workaround or something similar to make it work with TypeScript.

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 its intended way, I apologize as I am still new to web development.

The following code snippet is what I attempted in TypeScript but unfortunately did not work as expected:

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

button?.addEventListener("click", buttonClicked)

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

Answer №1

I found a workaround by bypassing the error.

let customButton : HTMLElement | null= document.getElementById('myCustomButton');

customButton?.addEventListener("click", buttonClick)

function buttonClick()
{
    console.log("Clicked");
    
    // @ts-ignore
    eel.my_custom_python_function();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting issues with accessing the _id property using Typescript in an Angular application

Encountering an Angular error that states: src/app/components/employee/employee.component.html:67:92 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is ...

Why is my JQuery code refusing to function even though I have coded it correctly?

Encountering an issue with the carousel functionality. The button is supposed to switch between play and pause, triggering the carousel accordingly. However, upon clicking the button, it seems to switch too quickly without properly pausing or playing the c ...

Guide on tallying a unique value of a chosen option within a table using jQuery

I have select elements in my table. I am trying to determine if any of the selected options in those select elements have the value of optional. If any of the select elements has optional selected, then I want to display a text field below it. However, I a ...

A method for arranging an array of nested objects based on the objects' names

Recently, I received a complex object from an API: let curr = { "base_currency_code": "EUR", "base_currency_name": "Euro", "amount": "10.0000", "updated_date": "2024 ...

What is the best way to determine if a value from my array is present within a different object?

I have an array with oid and name data that I need to compare against an object to see if the oid value exists within it. Here is the array: const values = [ { "oid": "nbfwm6zz3d3s00", "name": "" ...

Analyzing the contents of a JSON file and matching them with POST details in order to retrieve

When comparing an HTTP Post body in node.js to a JSON file, I am looking for a match and want the details from the JSON file. I've experimented with different functions but I'm unsure if my JSON file is not formatted correctly for my needs or if ...

Trigger the submission of Rails form upon a change in the selected value within a dropdown form

I am working on a Rails application that has a table of leads. Within one of the columns, I display the lead's status in a drop-down menu. My goal is to allow users to change the lead's status by simply selecting a different value from the drop-d ...

Position an HTML div by using data from a JSON object

While working on my dashboard, I successfully saved data to my database using jQuery, JSON, and a generic handler. Subsequently, I managed to retrieve this data using a WebService and JSON. The structure of my database information is as follows: Upon docu ...

Executing `removeChild` within a timeout during page load does not yield the expected results

I have an HTML div that is designed to contain dynamically generated children. These children are meant to be removed from the list after a specific amount of time (e.g. 1000 ms). Although some people have experienced scope issues with timeout functions, ...

Is it possible to display Angular Material Slider after the label?

Searching through the Angular Material docks, I came across the Sliders feature. By default, the slider is displayed first, followed by its label like this: https://i.sstatic.net/C5LDj.png However, my goal is to have the text 'Auto Approve?' sh ...

Introduction to Angular controller binding for beginners

I'm currently going through this tutorial : http://www.youtube.com/watch?v=i9MHigUZKEM At 46:32 minutes into the tutorial, this is the code I have implemented so far : <html data-ng-app="demoApp"> <body data-ng-controller="SimpleControlle ...

Modify a single field in user information using MySQL and NodeJS

I am currently working on developing a traditional CRUD (create, read, update, delete) API using NodeJS/Express and MySQL. One of the routes I have created is responsible for updating user information, which is functioning correctly. The issue: When I d ...

Using jQuery to append a single AJAX response at a time

I wrote a piece of code that utilizes an ajax request to communicate with json-server, retrieving data which is then displayed in an html div using jquery .html(). The content shown in the div depends on the button clicked by the user. While .append() work ...

Ways to swap out a React component for a different one after modifying its state

I'm relatively new to React and I am currently working on a project where I need to display a large image that takes 3-4 seconds to load. To enhance user experience, I plan to implement a loader using the ReactImage component available at https://www. ...

Changes in model not reflected in the view

In my Angular app (5.2.3), I have implemented a feature to display a login/logout button in the top right corner of the page. The functionality involves silently logging the user in using an external Open ID Connect provider and then displaying the user&ap ...

Tips for Including a Parallax Image Within a Parallax Section

Currently, I am working on implementing a parallax effect that involves having one image nested inside another, both of which will move at different speeds. My progress has been somewhat successful, however, the effect seems to only work on screens narrowe ...

Iterate through an array of objects, applying a filter and simultaneously generating a new object

I have an array of elements structured like the example below. In this structure, there are nested rows within input, and each rows object contains an array of objects. let input = [ { "title": "ENGINEERING", "rows": [ { "ri ...

Leveraging periods within a MySQL database: Node.js for seamless updates

I am currently facing a challenge in updating a column name that contains a period in node using node-mysql. I appreciate the convenience of being able to update multiple columns by providing an object with keys, but the string escaping process with node-m ...

JavaScript: void(0), Internet Explorer 6, and SWFAddress are all important components

Hello there, We are on the verge of launching a secure website (regrettably, we cannot provide the URL) and have come across a rather obscure bug in IE6 that I am hoping someone may have experienced or could shed some light on. This issue only arises when ...

Ionic 2's Navigation Feature Failing to Function

I need to implement a "forgot password" feature on my login page. When a user clicks the button, they should be redirected to the "forgot password" page. Below is the code snippet from my login.html <button ion-button block color="blue" (cli ...