-2

I wrote the below code to invoke python code from Javascript code, i.e on press of some button, But when I run this, I get the full python file as output instead of execution of that file. Can someone guide me how to fix that.

Got a similar question here, but could not understand the solution : Triggering Python script using AJAX Javascript request on local server using vanilla JS

My code :

const xhttp = new XMLHttpRequest();
                      xhttp.onreadystatechange = function() {
                        if (this.readyState == 4 && this.status == 200) {
                          alert(this.responseText);
                            
                            alert("hello from js");
                        }
                      };
                      xhttp.open("GET", "uploadFile.py");
                            
                      xhttp.send();

Can someone please help me ?

Regards

  • Do I need to make my machine a local server running Python on some port ? DO i need to run my that particular script at that port or just python .. I'am very new to server side actually and hence my questions may be very basic .. please bear with me .. – lotus airtel300 Jun 20 '21 at 11:43
  • you can send data to python from JS and receive back but what do you want to accomplish from that . for proper use you have to use some framework like Flask or Django. running basic http server won't help because making GET or POST request become hard. – gaurav Jun 20 '21 at 11:48

1 Answers1

0

It seems that the thread that you were referencing did not name the question properly, and thus you mistook that the NodeJS process can run python scripts.

This is also assuming that you want to run the python script on your nodeJS process and not on the browser, as the browser's javascript engine would not know what to do with a .py file.

The NodeJS process cannot execute python scripts, however the python3 (assuming it is installed on your server/environment) can. So the only way to do is to get NodeJS to tell the shell environment to get python3 to run the python script.

Referencing https://stackoverflow.com/a/52575123/4036876, one of the ways you can achieve it is by using execSync or exec to run a main.py via python3:

const execSync = require('child_process').execSync;
// import { execSync } from 'child_process';  // replace ^ if using ES modules
const output = execSync('python3 main.py', { encoding: 'utf-8' });  // the default is 'buffer'
console.log('Output was:\n', output);
mecode4food
  • 303
  • 1
  • 13
  • Also to continue on the answer respond via a comment on the answer instead of your question – mecode4food Jun 20 '21 at 11:47
  • one question : will require work in plain javascript ? when i used the above code, it gave me error : Uncaught Error: Module name "child_process" has not been loaded yet for context: _. Use require([]) – lotus airtel300 Jun 20 '21 at 12:13
  • You have to run it in your node process, not on your browser. – mecode4food Jun 20 '21 at 12:23
  • But I'am not using node, I'am using Plain HTML javascript – lotus airtel300 Jun 20 '21 at 12:32
  • As mentioned, your browser knows _nothing_ about how to deal with python scripts. You will have to either bundle the python script in a function and serve via flask/django that runs the function in response to an incoming request, or run your js on a node process in an environment that has python. – mecode4food Jun 20 '21 at 12:40