Flask is a micro - framework for Python. It provides a simple way to create web applications and APIs. Key concepts in Flask include routes, which define the URLs that the application will respond to, and views, which are functions that handle requests for those routes. Flask also supports features like request handling, response generation, and session management.
from flask import Flask
# Create a Flask application instance
app = Flask(__name__)
# Define a route and its corresponding view function
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
React is a JavaScript library for building user interfaces. It uses a component - based architecture, where the UI is broken down into small, reusable components. React uses a virtual DOM (Document Object Model) to optimize rendering performance. Components can have their own state and props, which allow them to manage and pass data respectively.
import React from'react';
import ReactDOM from'react - dom/client';
// Define a functional component
function HelloWorld() {
return <h1>Hello, World!</h1>;
}
// Render the component to the DOM
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<HelloWorld />);
python -m venv venv
source venv/bin/activate # For Linux/Mac
.\venv\Scripts\activate # For Windows
pip install flask
app.py
file with the following content:from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/data')
def get_data():
data = {'message': 'This is sample data from the Flask backend'}
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)
npx create - react - app my - react - app
cd my - react - app
npm start
fetch
API or a library like axios
to make requests to the Flask backend.import React, { useEffect, useState } from'react';
function App() {
const [data, setData] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch('http://127.0.0.1:5000/api/data');
const jsonData = await response.json();
setData(jsonData);
} catch (error) {
console.error('Error fetching data:', error);
}
};
fetchData();
}, []);
return (
<div>
{data? (
<p>{data.message}</p>
) : (
<p>Loading...</p>
)}
</div>
);
}
export default App;
flask - cors
library in the Flask application.pip install flask - cors
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/api/data')
def get_data():
data = {'message': 'This is sample data from the Flask backend'}
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)
Integrating Flask with React provides a powerful combination for building full - stack web applications. Flask’s simplicity and flexibility make it a great choice for backend development, while React’s component - based architecture and performance optimizations are ideal for creating dynamic user interfaces. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, developers can effectively build and deploy full - stack applications using these technologies.