how do I use my python script in a typescript file? Like how do I link it ? I have to traverse a xml file which I have a python code which updates some details and wanted to use it in the typescript server file.
Like the server should update some details in a xml file. this update functionality is implemented in python and I wanted to use it in the typescript server code.
how do I use my python script in a typescript file? Like how do I link it ? I have to traverse a xml file which I have a python code which updates some details and wanted to use it in the typescript server file.
Like the server should update some details in a xml file. this update functionality is implemented in python and I wanted to use it in the typescript server code.
Share Improve this question edited Aug 16, 2022 at 19:54 vinayak asked Aug 16, 2022 at 19:50 vinayakvinayak 231 gold badge1 silver badge5 bronze badges 3- Transpile (port) the Python script into TypeScript? Or run it through the shell? – k.tten Commented Aug 16, 2022 at 19:52
- 1 You can create a new process and call the python code. Look into inter-process munication. This can get messy with passing arguments and getting output back. You may be better of rewriting the Python code in TypeScript. – Robert Commented Aug 16, 2022 at 19:52
- See if this is want you need: stackoverflow./questions/30689526/… – PM 77-1 Commented Aug 16, 2022 at 19:53
1 Answer
Reset to default 3You can run the python script in node using exec()
: https://nodejs/api/child_process.html#child_processexecmand-options-callback
A minimal example assuming you have python3
installed on the machine running your node script could be:
// shell.py
print('printed in python')
// server.js
import {exec} from 'child_process'
//if you don't use module use this line instead:
// const { exec } = require('child_process')
exec('python3 shell.py', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
}
else if (stderr) {
console.log(`stderr: ${stderr}`);
}
else {
console.log(stdout);
}
})
// using it in the shell
% node server.js
// should output this:
printed in python
This way you could traverse your XML file, change it and output it to the js/ts file, or save it as a file and just read that via your js/ts code.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772523a4592845.html
评论列表(0条)