The Question:
I am developing a disease prediction website. I have used react for frontend and express, mongodb, nodejs for backend. If run the python script just by executing the .js (file containing the Python-Shell code) file it works just fine. But if I run .js file in a post request in nodejs when a webpage loads in react frontend using useEffect the python script is not executing.
Any relevant python/javascript code:
JAVASCRIPT(NODEJS)
1. const {PythonShell} = require('python-shell')
2. const router = require('express').Router()
3. const spawn = require('child_process').spawn
4. router.post('/', async(req, res ) => {
5. //METHOD -1 (Not Working)
6. try {
7. options = {
8. pythonPath: 'C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python39\\',
9.
10 args: [req.body[0]], //req.body[0] indicates the command line argument for the python program
11
. scriptPath:'C:\\Users\\default\\Documents\\getSymptoms.py' //path of my .py file
12. };
13. PythonShell.run('./getSymptoms.py', options, function (err, results) {
14. if (err) throw err;
15. return res.send(results);
16. });
17.
18. //METHOD -2 (Not working too)
19. var output
20. const python = spawn('python.exe', ['./getSymptoms.py',req.body[0]])
21. // PART-1 (Not working)
22. python.stdout.on('data', function (data) {
23. console.log('Pipe data from python script ...');
24. output = data.toString();
25. });
26. //PART-2 (***WORKING***)
27. python.on('close', (code) => {
28. console.log(output);
29. console.log(`child process close all stdio with code ${code}`);
30. // send data to browser
31. return res.send(output);
32. });
33. }
34. catch (error) {
35. return res.send({ message: "Error" });
36. }
37. })
38. module.exports=router
39.
PYTHON FILE
print(sys.argv[1])
Reactions are currently unavailable
The Question:
I am developing a disease prediction website. I have used react for frontend and express, mongodb, nodejs for backend. If run the python script just by executing the .js (file containing the Python-Shell code) file it works just fine. But if I run .js file in a post request in nodejs when a webpage loads in react frontend using useEffect the python script is not executing.
Any relevant python/javascript code:
JAVASCRIPT(NODEJS)
1. const {PythonShell} = require('python-shell') 2. const router = require('express').Router() 3. const spawn = require('child_process').spawn 4. router.post('/', async(req, res ) => { 5. //METHOD -1 (Not Working) 6. try { 7. options = { 8. pythonPath: 'C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python39\\', 9. 10 args: [req.body[0]], //req.body[0] indicates the command line argument for the python program 11 . scriptPath:'C:\\Users\\default\\Documents\\getSymptoms.py' //path of my .py file 12. }; 13. PythonShell.run('./getSymptoms.py', options, function (err, results) { 14. if (err) throw err; 15. return res.send(results); 16. }); 17. 18. //METHOD -2 (Not working too) 19. var output 20. const python = spawn('python.exe', ['./getSymptoms.py',req.body[0]]) 21. // PART-1 (Not working) 22. python.stdout.on('data', function (data) { 23. console.log('Pipe data from python script ...'); 24. output = data.toString(); 25. }); 26. //PART-2 (***WORKING***) 27. python.on('close', (code) => { 28. console.log(output); 29. console.log(`child process close all stdio with code ${code}`); 30. // send data to browser 31. return res.send(output); 32. }); 33. } 34. catch (error) { 35. return res.send({ message: "Error" }); 36. } 37. }) 38. module.exports=router 39.PYTHON FILE
print(sys.argv[1])