Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

node.js - How to call node functions from python and vice versa

I was previously able to establish communication between javascript and python scripts by using a python library called Eel. However, I began to have issues with Eel when I needed to integrate it with the Electron app. So I am now trying to use sys.stdout in Electron to communicate with python script. Here is my code

Electron App

Main.js

const spawn = require("child_process").spawn
const pythonProcess = spawn('python3', ["./py/test.py"])


pythonProcess.stdout.on('data', (data) => {
   mydata = data.toString()
   console.log('we have data from python ',mydata)
})


function getDataFromPY(data){

   console.log("i've got data")


 }

test.py

   def testNode():
      print("node has called this python function")


   def helloNode():
       print("hello from python " 


   helloNode()

I was able to start the test.py file from the node but I want to be able to call node function from python and vice versa. What is the best method to call specific functions in node from python and vice versa in Electron framework?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a helpful library called [Naked][1] (Github page).

from Naked.toolshed.shell import execute_js, muterun_js

results = execute_js('AnyScriptFile.js')

if result:
    #success
else:
    #failure

There are many other methods out there. But I have tried this and it has worked for me. [1]: https://github.com/chrissimpkins/naked


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...