1

I'm attempting to run python script through an html button for my electron app. However, I'm having issues with connecting the python code to the html button.

I'm using visual studio code for JavaScript and python. All the files are saved in the same folder/directory.

Here's my python code (script.py):

import sys


num1 = 3.2
num2 = 4.1

sum = float(num1) + float(num2)

print("The sum of {0} and {1} is {2}" .format(num1, num2, sum))

num3 = 5.3
num4 = 6.9

sum2 = float(num3) * float(num4)

print("The sum of {0} and {1} is {2}" .format(num3, num4, sum2))
sys.stdout.flush()

Here's my HTML code (Wifi.html):

<!DOCTYPE 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">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" media="screen" href="main2.css" />
    <script src="g/script.js"></script>
</head>
<body>
    <h1>A</h1>
        <br>
    <h2>W</h2>



 <ul>
        <li><button id="add" class="btn btn-warning" onclick="get_script()">Run Test</button></li>



 </ul>

 <div class=back>
      <input type=button onClick="location.href='DoS.html'" value='BACK'>
 </div>

</body>
</html>

Here is the JavaScript file I'm trying to connect python with JavaScript/electron:

function get_script(){
    var python = require("python-shell")
    var path = require("path")

    var options = {

        scriptPath : path.join(/g/script.py'),
        pythonPath : '/path'

}

var pleasewrk = new python("script.py", options);

pleasewrk.on('message', function(message){
    swal(message);
})

}

I would like for python code to be run and appear on either the same or pop-up page.

Dwane
  • 21
  • 3
  • Host the Python file on a website and have JavaScript make an AJAX request to it. Then you can use the `response`. – Obsidian Age Oct 15 '19 at 20:16
  • Possible duplicate of [Flask - Calling python function on button OnClick event](https://stackoverflow.com/questions/42601478/flask-calling-python-function-on-button-onclick-event) – mwilson Oct 15 '19 at 20:24
  • Possible duplicate of [How to call Shell script or python script in from a Atom electron app](https://stackoverflow.com/questions/35079548/how-to-call-shell-script-or-python-script-in-from-a-atom-electron-app) – David Culbreth Oct 15 '19 at 20:28

3 Answers3

1

You can child process module of node.js or python-shell module

Sudhakar Ramasamy
  • 1,736
  • 1
  • 8
  • 19
1

And if you are going to check the video indicated by @kingsley solomon, please note that after release 5.0.0 of electron you have to set nodeIntegration parameter to true (the video was made with an earlier version). Check out this SO question: electron 5.0.0 "Uncaught ReferenceError: require is not defined"

emaimone
  • 46
  • 3
0

You can't do that. Python does not execute in the browser. However, what you can do is use something like flask to make an accessible endpoint that accepts your parameters. Once the user selects the button on your page, it would then use node or javascript to send an HttpRequest to your python app.

I would argue the use case of using two separate server-side frameworks/languages here though. Unless you're doing something that absolutely only works in Python (or Node), I would just pick one or the other and stick with it. They are both highly capable of creating API's. Electron runs on Node, so I'd just stick with that (IMO).

mwilson
  • 12,295
  • 7
  • 55
  • 95