You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reactjs is one of the best frontend libraries for building frontend single-page applications. It is been developed and maintained by Facebook with a large community.
Flask is a backend micro-framework written in Python for the rapid development process. It is famous for its simplicity and independence. It does not need any external library for work, which makes it beginner-friendly, and many people choose this framework. Flask is generally used for building a REST API.
Let’s see a Step-by-step guide to connect Flask API with React JS to show the data on the web page.
Steps to connect React with Flask API and set up the Project :
Note : Make sure to make 2 separate folders for clean understanding, one for the Flask backend and one for the React frontend. The project structure should look like
Step 1: Setting up a flask server
Make a folder named backend and file server.js with the following command:
mkdir backend
cd backend
touch server.py
Build a basic flask server. Write down the following code in server.py file.
# Filename - server.py# Import flask and datetime module for showing date and timefromflaskimportFlaskimportdatetimex=datetime.datetime.now()
# Initializing flask appapp=Flask(__name__)
# Route for seeing a data@app.route('/data')defget_time():
# Returning an api for showing in reactjsreturn {
'Name':"geek",
"Age":"22",
"Date":x,
"programming":"python"
}
# Running appif__name__=='__main__':
app.run(debug=True)
Step to run the application: Use the following command to run the server:
python server.py
Step 2: Setting up the Reactjs project
Make a react project using the following command:
yarn create react-project frontend // OR
npx create-react-app frontend
Move After creating the app, move into the app using the following command:
cd frontend
After that open package.json and add the proxy.
"proxy":"http://localhost:5000/"
Now, we provide the proxy in react package.json file because we need to access the flask URL in order to get the API from our react app. In general what proxy does is, when we request into the javascript web server which serves the react frontend will automatically be redirected to the proxy key. In this case, it will be our flask server.
Step 3: Fetching the API
For fetching the API useState and useEffect hooks are used in react app.
useState: It is used for setting a data from the API and providing into the jsx for showing the data. useEffect: It is used for rendering a fetch method on a single reload.
How to connect ReactJS with flask API ?
Reactjs is one of the best frontend libraries for building frontend single-page applications. It is been developed and maintained by Facebook with a large community.
Flask is a backend micro-framework written in Python for the rapid development process. It is famous for its simplicity and independence. It does not need any external library for work, which makes it beginner-friendly, and many people choose this framework. Flask is generally used for building a REST API.
Let’s see a Step-by-step guide to connect Flask API with React JS to show the data on the web page.
Steps to connect React with Flask API and set up the Project :
Note : Make sure to make 2 separate folders for clean understanding, one for the Flask backend and one for the React frontend. The project structure should look like
Step 1: Setting up a flask server
Make a folder named backend and file server.js with the following command:
Build a basic flask server. Write down the following code in server.py file.
Step to run the application: Use the following command to run the server:
python server.py
Step 2: Setting up the Reactjs project
Make a react project using the following command:
Move After creating the app, move into the app using the following command:
cd frontend
After that open package.json and add the proxy.
"proxy":"http://localhost:5000/"
Now, we provide the proxy in react package.json file because we need to access the flask URL in order to get the API from our react app. In general what proxy does is, when we request into the javascript web server which serves the react frontend will automatically be redirected to the proxy key. In this case, it will be our flask server.
Step 3: Fetching the API
For fetching the API useState and useEffect hooks are used in react app.
useState: It is used for setting a data from the API and providing into the jsx for showing the data.
useEffect: It is used for rendering a fetch method on a single reload.
Step to run the application: Use the following command to run the server:
Output: This output will be visible on the http://localhost:3000/ on the browser window.
The text was updated successfully, but these errors were encountered: