DEV Community

SMAmmar
SMAmmar

Posted on

Python to React

Hello guys
I have performed sentiment analysis in python using Vader library,but now I want to send this data to react front end.
How can I do it ?

Top comments (10)

Collapse
 
manan30 profile image
Manan Joshi

Hello, you might want to set up a python flask API endpoint and make a request to that endpoint from your React app to fetch the data.

Collapse
 
smammar profile image
SMAmmar

Can't I do it using node.js

Collapse
 
jkhaui profile image
Jordy Lee

Why would you write an API in node.js if the sentiment analysis is a python script?

Thread Thread
 
smammar profile image
SMAmmar

Cause I want to deploy through MERN stack my application.Also I don't know if sentiment analysis can be done in tensorflow or not

Thread Thread
 
jkhaui profile image
Jordy Lee • Edited

Ah okay. Not sure if I fully understand your situation, but here's what I suggest:

Write your python sentiment analysis script as a serverless function (e.g. for AWS Lambda). Build an API for the script and call the API endpoint from the React Frontend. The rest of your app can follow the MERN stack.
As far as I understand, you'll have to build an API for the python script no matter what; I don't see how it can communicate with node or react otherwise.
E.g. if your app must absolutely follow the MERN stack, the React layer would read from the node endpoint... Which would itself get data from the Python API. So why not just call the Python API directly?

If you think this is a viable approach, I can share some boilerplate code I use to make serverless python functions on AWS Lambda with API Gateway

Thread Thread
 
smammar profile image
SMAmmar

Sure @jordan Lee, It would really help me

Thread Thread
 
jkhaui profile image
Jordy Lee

Sure :) But a heads-up first: please make sure you're willing to spend a bit of time learning AWS Lambda + API gateway and check that it's relevant to your needs. I don't want to accidentally send you down the wrong path if there's a better way to do it.

Anyway, here's the repo: github.com/jkhaui/serverless-pytho...

  1. Use the command npm i serverless -g to install the serverless framework globally.
  2. Clone the repo I sent you and follow instructions. Run serverless offline to edit your Python script locally. Test the API at localhost:4000
  3. If all goes well, use serverless deploy to host it on AWS Lambda. An endpoint URL should also be created automatically through API gateway.

Any questions/problems, let me know. Also, you'll be prompted for your AWS credentials at some point during these steps

Thread Thread
 
jkhaui profile image
Jordy Lee

Oh and btw, you can delete all the Spacy code in handler.py and also delete the directory en_core_web_sm-2.1.0. That's just for demo purposes.

Put your Python script in handler.py. This is the code that will let your script be accessed as an API:

    response = {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*',
        },
        'body': json.dumps(entities)
    }

    return response
Thread Thread
 
smammar profile image
SMAmmar

sure Jordan, First I will also look into the alternatives. But I highly appreciate the help you provided :).Wish you good luck in your future endeavors.

Thread Thread
 
jkhaui profile image
Jordy Lee

No problem :) Thank you, and best of luck with your app too.