Not getting response from Python backend on button click

I have this minimal electron app, which communicates with the python backend on clicking a button and logs the response in the console. The issue is that the result is logged twice every time the button is clicked twice. Nothing happens when the button is clicked once, although I have noticed (using ps aux) that a child process is indeed spawned.
How can I fix this (get the result from python every single time the button is clicked)?

I made this github gist containing the relevant code. One just needs to run npm install and npm start to run the application.

I have tried the gist … and it requires two clicks as you write.

Could you use window.onload to make the code run only after page has loaded … from what I have read from searching.

@d_l Thank you for reaching out. Do you mean to add that in weather.js like this (gave an id to the button and removed “onclick” attribute):

const { PythonShell } = require('python-shell')
function get_weather() {
    var data = "Cool Weather\n"
    let pyshell = new PythonShell('weather.py')
    pyshell.send(data)
    pyshell.on('message', function (message) {
        console.log(message+" HI")
    })
    pyshell.end(function (err, code, signal) {
        if (err) throw err;
        console.log(code + ' ' + signal);
    })
}

window.onload = (event) => {
    
var btn = document.getElementById("btn");
    btn.addEventListener('click', () => {
    get_weather();
  });
}

If yes, then this doesn’t work either. Also, I have noticed that, if I pass the input as an argument and get it as sys.argv in the python program, then it works. But, since my data is large, I can’t use that.

I was just casually looking around and found this thread. <<<-- click
I am interested in following the outcome since I’m considering building an Electron app with python backend. My data is also large.