Creating a project that combines Flask as the backend and React as the frontend can be a great way to build a modern web application with a powerful backend and a dynamic frontend. In this tutorial, we will walk through the process of creating a Flask + React project step by step.
Step 1: Setting up the backend with Flask
-
First, make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/downloads/).
-
Install Flask by running the following command in your terminal:
pip install Flask
-
Create a new directory for your project and navigate to it. Inside the directory, create a new Python file called
app.py
. This will be our Flask application file. - Open
app.py
in your text editor and add the following code to create a basic Flask application:from flask import Flask
app = Flask(name)
@app.route(‘/’)
def index():
return ‘Hello, World!’
if name == ‘main‘:
app.run(debug=True)
5. Save the file and run it by executing the following command in your terminal:
```bash
python app.py
- Open your web browser and navigate to
http://127.0.0.1:5000/
. You should see the message "Hello, World!" displayed on the page.
Step 2: Creating the frontend with React
-
Install Node.js if you haven’t already. You can download Node.js from the official website (https://nodejs.org/).
-
Create a new directory for your frontend inside the project directory. Open a terminal, navigate to the frontend directory, and run the following command to create a new React app:
npx create-react-app frontend
-
Navigate to the frontend directory by running
cd frontend
, and then start the React development server by runningnpm start
. - Open your web browser and navigate to
http://localhost:3000/
. You should see the default React app running.
Step 3: Connecting the frontend and backend
-
In order for the frontend to communicate with the backend, we need to set up CORS (Cross-Origin Resource Sharing) in our Flask application. Install the Flask-CORS package by running the following command:
pip install flask-cors
- Import and configure Flask-CORS in
app.py
by adding the following lines of code:from flask_cors import CORS
app = Flask(name)
CORS(app)
3. Modify the route in `app.py` to accept GET requests and return JSON data:
```python
from flask import jsonify
@app.route('/')
def index():
return jsonify(message='Hello, World!')
Step 4: Using Axios to fetch data from the backend in React
-
Install Axios in the frontend directory by running the following command:
npm install axios
-
Open
App.js
in the frontend directory and import Axios at the top of the file:import axios from 'axios';
-
Create a new state variable to store the data fetched from the backend, and use Axios to fetch data in the
componentDidMount
lifecycle method:class App extends Component { state = { message: '' } componentDidMount() { axios.get('http://127.0.0.1:5000/') .then(response => { this.setState({ message: response.data.message }); }); }
// Rest of the code
}
4. Render the message in the `render` method in `App.js`:
```javascript
render() {
return (
<div>
<h1>{this.state.message}</h1>
</div>
);
}
- Save the changes and navigate to
http://localhost:3000/
in your web browser. You should see the message "Hello, World!" fetched from the Flask backend.
Congratulations! You have successfully created a Flask + React project with a Python backend and React frontend. You can now start building your web application by adding new routes and components to your project.
my npx is not working its showing its not recognized what to do please help!!
I must running both the Flask app and the react app at the same time right
Great video, an issue I was running into was using localhost for the JSON file, and so when I replaced localhost:3000 with 127.0.0.1, and after loading Localhost:3000 this seemed to work. Awesome job!
Thank you, man! I'm from Brazil and don't found nothing in my language so good like this video. You Are great!
Which is best mern or react flask mysql
This guys going places
Actually it helped me a lot, thanks bro
Wow❤
awesome tutorial
Simple and to the point with fabulous explanation ❤
thanks for creating this video
இலட்சியத்தை அடைய அமைதியான வழிகளை பின்பற்ற வேண்டும். பலாத்கார புரட்சி தேவையில்லை!
-காமராசர்
it gets console logged twice for me, why do you think this is happening?
what's the plugin you're using that shows you the size of the react import or whatever? as well as the one that allowed the quick sample upload? I tried and those features didn't work for me. But tutorial is overall good and concise, love it
venv is just for consistency of environment?
What theme is that ?
Amazing video, many thanks!!! Keep doing this great job.
simple and clear .Thanks
Really amazing and easy to follow, just one question, If a user uses the network developer tool, The backend API can be exploited and scraped, How can We hide it, or avoid this situation
Short and sweet. thanks