I have node.js installed and want to know if there is any way to run js code on python 3.8. I need to use a large piece of code (babylon) for my web application. Is there any easy way to do this?
2 Answers
Running Code
I admit I have not messed with this before, but I think the best way to go about it is to have a js file that you can run from python. I found 2 ways to accomplish this- one that returns output and one that simply just runs it.
Getting Output
You may be able to use the subprocess module to run the node and record it's output for later use.
import subprocess
myValue = subprocess.check_output(['node','index.js'])
Python subprocess.check_output Documentation
No Output
If you do not require to read the output of the file, you can just run it with the os module.
import os
os.system("node index.js")
Comments
I do like the answer given. I would like to add that one could use the Naked library:
from Naked.toolshed.shell import execute_js
execute_js('file.js')
One can read more here: https://naked.readthedocs.io/