Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine javascript/react frontend and python backend?


I'm not quite sure if my question is a duplicate, but I wasn't able to find something helping me in my case.

Set Up
I've built a frontend webpage which contains a couple of services, for example show some timeseries and other information about my system. The website is build with the react framework and so using javascript in general.
Now I want to do some calculations about the timeseries for example calculate the similarity and other features of my sensordata. For that I'm using python which offers me a lot of libraries I've used for a long time and are easy to use.


What I'm looking for:
I'm looking for a very simple way to call my backend-timeseries-analysis-python script from the react GUI passing some variables like the length of the series. Also I want to process the returned values and safe the current values needed for normalization (like max,min) for further calculations.

So the procedure would look like the following:

1) Type value in react frontend input box
2) react/javascript calls pythonscript/ initialize a class and passes variables to class
3) python calculates similarity of sensor data 
4) python returns similarity values to frontend and saves classes for later call
5) react displays returned values
6) react/javascript calls pythonscript
7) python compares latest data to past data and refreshs treshholds(like max, min)
8) python calculates similarity of sensor data 
9) continue.. 

Thanks for your help!

System set up

like image 363
nail Avatar asked Mar 04 '20 14:03

nail


People also ask

Can I use JavaScript for frontend and Python for backend?

Running the Python backend with JavaScript frontend Make sure the backend server is running by running the command python app.py in the terminal/command prompt in the backend directory. Then start the frontend web server if it is not running in the frontend directory: http-server .

Can you use React and Python together?

Build a Simple CRUD App with Python, Flask, and React shows how to combine a Flask backend with React. Learn React app is a Git repository with a code tutorial and instructions for how to follow along, as well as exercises to ensure you are tested as you go.

How do you connect frontend and backend in react JS?

export default App; Now run the Nodejs process npm run dev in one terminal and in another terminal start Reactjs using npm start simultaneously. Output: We see react output we see a button “Connect” we have to click it. Now when we see the console server-side we see that the ReactJS is connected with NodeJS.


2 Answers

You can expose your Python scripts on a REST API which will be called by your React frontend. Database connection will be made by this API, and the response is sent to your frontend.

See Flask (very simple for small projects) or even Django to build Python APIs.

like image 109
Sayydika Avatar answered Oct 07 '22 23:10

Sayydika


Check out Streamlit. It's a very good alternative to Flask /Django. I am a backend python guy and have no experience in using frontend but I was able to spin up the front end quickly with Streamlit.

like image 33
Arun Avatar answered Oct 07 '22 22:10

Arun