Creating a Flask + React Project with Python Backend and React Frontend

Posted by


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

  1. First, make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/downloads/).

  2. Install Flask by running the following command in your terminal:

    pip install Flask
  3. 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.

  4. 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
  1. 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

  1. Install Node.js if you haven’t already. You can download Node.js from the official website (https://nodejs.org/).

  2. 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
  3. Navigate to the frontend directory by running cd frontend, and then start the React development server by running npm start.

  4. 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

  1. 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
  2. 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

  1. Install Axios in the frontend directory by running the following command:

    npm install axios
  2. Open App.js in the frontend directory and import Axios at the top of the file:

    import axios from 'axios';
  3. 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>
    );
}
  1. 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.

0 0 votes
Article Rating
24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@janmejay1306
3 months ago

my npx is not working its showing its not recognized what to do please help!!

@Patssion
3 months ago

I must running both the Flask app and the react app at the same time right

@syeds4380
3 months ago

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!

@sf-zr3oj
3 months ago

Thank you, man! I'm from Brazil and don't found nothing in my language so good like this video. You Are great!

@Kiran23456
3 months ago

Which is best mern or react flask mysql

@GodlygalliganDeadInTheBrain
3 months ago

This guys going places

@alejandroortega25
3 months ago

Actually it helped me a lot, thanks bro

@AbhigyanPawar
3 months ago

Wow❤

@hazehaze7820
3 months ago

awesome tutorial

@Engr.SharoonHoney
3 months ago

Simple and to the point with fabulous explanation ❤

@CodeMastersChannel
3 months ago

thanks for creating this video

@user-eb8lk9wg1w
3 months ago

இலட்சியத்தை அடைய அமைதியான வழிகளை பின்பற்ற வேண்டும். பலாத்கார புரட்சி தேவையில்லை!
-காமராசர்

@millpreetk1406
3 months ago

it gets console logged twice for me, why do you think this is happening?

@millpreetk1406
3 months ago

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

@millpreetk1406
3 months ago

venv is just for consistency of environment?

@achrafkandoussi9525
3 months ago

What theme is that ?

@felipedepra2745
3 months ago

Amazing video, many thanks!!! Keep doing this great job.

@PaulThiongo-rv7so
3 months ago

simple and clear .Thanks

@5enox
3 months ago

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

@alireza2295
3 months ago

Short and sweet. thanks