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?

1
  • 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 Commented Sep 16, 2018 at 2:17

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

@WaaberiIbrahim updated with some extra info. hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.