Using Eel calling JavaScript function in python only when a button is clicked
See original GitHub issueDescribe the problem I am trying to call a JavaScript function but only when a button is clicked , currently I am able to call JavaScript function but it trigger’s as soon as the UI is opened how can I avoid it as am new to Eel
Expected When btn1 is clicked function name2 should fire in python code i.e just want to end value from js to python when btn1 in clicked
Code snippet(s) Here is some code that can be easily used to reproduce the problem or understand what I need help with.
import time
import eel
eel.init("web")
@eel.expose
def printFromPython():
return "hello from python"
@eel.expose
def getTime():
return time.strftime('%c')
#creating a python function to use js returned vals
def print_num(n):
result = n
print('Got this from Javascript:', result)
print(result['c'])
print(result['un'])
print(result.keys())
print('Calling Javascript...')
eel.name2()(print_num)
eel.start('main.html')
...
<html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="Description" content="Enter your description here" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<title>Simple Demo</title>
</head>
<body>
<div>
<h3 class="text-center alert-info">Just A Simple Demo</h3>
<p class="text-center alert-danger" id="demo"></p>
<div class="container-fluid text-center ">
<input class="form-control btn btn-outline-success m-3" placeholder="Enter username" id="un">
<input class="form-control btn btn-outline-success m-3" placeholder="Enter password" id="pwd">
<button class="btn btn-primary m-3" id="btn1">Click Me Regular from JS</button>
</div>
</div>
<script type="text/javascript" src="/eel.js"></script>
<script src="main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
</body>
</html>
const d = new Date();
document.getElementById("demo").innerHTML = d;
function name1() {
var username = document.getElementById("un").value
var password = document.getElementById("pwd").value
alert(username + ":" + password);
document.getElementById("un").value = ""
document.getElementById("pwd").value = ""
}
eel.expose(name2);
function name2() {;
const c = "Hello lp from JS"
var username = document.getElementById("un").value
var password = document.getElementById("pwd").value
console.log(c)
return {
c: c,
un: username
pw: password
};
}
document.getElementById("btn1").addEventListener("click", name1)
Desktop (please complete the following information):
- OS: Windows
- Browser chrome
- Version 90
Issue Analytics
- State:
- Created 2 years ago
- Comments:11
Top Results From Across the Web
Using Eel calling JavaScript function in python only when a ...
I've solved it after lots of trail and error, since I'm new to JavaScript, Eel and connecting both with Python, I only later...
Read more >Using Eel calling JavaScript function in python only when a ...
Describe the problem I am trying to call a JavaScript function but only when a button is clicked , currently I am able...
Read more >Using Eel calling JavaScript function in python ... - Bountysource
Describe the problem. I am trying to call a JavaScript function but only when a button is clicked , currently I am able...
Read more >Javascript can`t see python function exposed with eel
Look at the below code, eel.start() should be in the end of the code: import eel. import pyowm. owm = pyowm.
Read more >Simple distributable desktop app with Python and Web - Medium
This is the magic of eel lib, by using eel.expose(# js function here) you can make it callable at your python script. Cool...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try this:
main.py
main.js
The key is that your Python code must expose functions that you’d like to call from JavaScript, just like you did with the
getTime()
. In your original code, JavaScript was trying to call functionpython_func
but the Python code did not expose any function calledpython_func
. Since you already had Python functionprint_fromjs
, I just exposed that in Python and called it from JavaScript.Just get the values from HTML and pass them to a python function eg