Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// javascript code
// ......
// python code here ...
// ....

What I have tried:

console.log("javascript code")
// python code here ....
console.log("another javascript code")
Posted
Updated 29-Aug-22 17:26pm
Comments
Mohibur Rashid 29-Aug-22 13:06pm    
It is possible to call a python script from javascript. Get the result in json format and parse it and assign to the desired javascript variable. But this will not work in browser.

No, you can't. The javascript engine in the browser will not run Python code.

And before you get the idea to launch an external process from the browser so an interpreter on the client machine can execute the Python code, you cannot do that either because of security concerns.
 
Share this answer
 
Comments
Patrice T 29-Aug-22 13:38pm    
"javascript (node) code" is not on server ?
Dave Kreskowiak 29-Aug-22 13:46pm    
The example given doesn't seem to be server-side code.
Patrice T 29-Aug-22 13:48pm    
Tittle says "node", I guess it matters.
Dave Kreskowiak 29-Aug-22 13:53pm    
Ah, didn't see it.
Yes, it is possible using childprocess core module. You need to call your Python script from Node.js which will store the output of your Python script and you can do whatever you want with the variable post it.

An example from web[^] is:
JavaScript
const express = require('express')
const app = express()

app.get('/', (req, res) => {

    const { spawn } = require('child_process');
    const pyProg = spawn('python', ['./../pypy.py']);

    pyProg.stdout.on('data', function(data) {

        console.log(data.toString());
        res.write(data);
        res.end('end');
    });
})

app.listen(4000, () => console.log('Application listening on port 4000!'))

Detailed step-by-step to set it up can be followed here: https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900