javascript - python script in a typescript file - Stack Overflow

how do I use my python script in a typescript file? Like how do I link it ? I have to traverse a xml fi

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
Add a ment  | 

1 Answer 1

Reset to default 3

You 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

相关推荐

  • javascript - python script in a typescript file - Stack Overflow

    how do I use my python script in a typescript file? Like how do I link it ? I have to traverse a xml fi

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信