0
   const pythonProcess = spawn('python',["tiqi.py", "textFile.txt"]);
    
        pythonProcess.stdout.on('data',(data)=>{
            console.log(data.toString());
        })

Can I call and run that code by clicking a component button in my react project?

1
  • 2
    If this is server-side code then your client-side code would likely make an AJAX request to a server-side resource to invoke the operation. Commented Jan 20, 2022 at 14:11

2 Answers 2

3

You cannot use node-based functions in a browser environment, to do such a thing in a proper way you should build an HTTP/HTTPS API for your react application and make requests from react to your API.

You can go through by googling building a simple node server based on express, and output your python script results in maybe .json file which you'll read with node (which will be sent on request from your react application).

Hope my answer will help you a bit.

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

Comments

1

Short answer: No. But other Node-based functions can be invoked using Browserify.

Refer to this link: https://github.com/browserify/browserify/issues/1816

Comments

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.