0

I want to send to python a node.js variable, modify it with python and resend it to node.js, that is an example of my codes: Python

def change_input(variable):
    variable*=5
    #now send variable to node.js

Now the js code:

var variable=prompt("enter the variable");
#after the code was sended to thd python code, modified and resend:
document.write(variable)

How to circulates the variables between python and javascript?

Waaberi Ibrahim
  • 639
  • 1
  • 4
  • 4
  • Start a web server with node.js express and make python request the server to get this variable and then send it back. Other than that you'd have to play around with interprocess communication. Read up on sockets and pipes – Danilo Souza Morães Sep 16 '18 at 02:17

1 Answers1

1

Here is one way to do it, by spawning a child python process from within node.

test.js:

const { spawn } = require('child_process');
const p = spawn('python', ['yourscript.py', '1']);

p.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

yourscript.py

import sys

def change_var(x):
    print('hello from python: {:d}'.format(int(x) + 2))

change_var(sys.argv[1])

output of running node ./test.js:

stdout: hello from python: 4

directory layout

➜  test tree
.
├── test.js
└── yourscript.py

0 directories, 2 files

related:

jmunsch
  • 16,405
  • 6
  • 74
  • 87