Flask is a lightweight Python web framework used to build:
-
Web applications
-
REST APIs
-
Backend services for mobile/web apps
-
Very simple and beginner friendly
-
Minimal setup (no heavy configuration)
-
Flexible – you choose the tools you need
-
Widely used for APIs and microservices
This means:
-
It provides only core features
-
URL routing
-
Request/response handling
-
-
Other features (database, authentication, etc.) are added using extensions
| Flask | Django |
|---|---|
| Lightweight | Full-stack framework |
| Easy to learn | Steeper learning curve |
| Best for APIs & small apps | Best for large applications |
| More control | More built-in features |
-
Client (Browser / Postman) sends a request
-
Flask matches the URL route
-
Python function (view) runs
-
Flask returns a response (HTML / JSON)
Client → URL → Flask Route → Python Function → Response
python --versionpython -m venv venvActivate:
- Windows
venv\Scripts\activate- Mac/Linux
source venv/bin/activatepip install flaskpython -c "import flask; print(flask.__version__)"from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)python app.pyOpen browser:
http://127.0.0.1:5000/
from flask import FlaskImports Flask class
app = Flask(__name__)Creates Flask application instance
@app.route('/')URL route (endpoint)
def home():
return "Hello, Flask!"Function that runs when URL is accessed
app.run(debug=True)Runs development server with auto-reload and error details
REST API allows applications to communicate using HTTP methods.
| Method | Purpose |
|---|---|
| GET | Retrieve data |
| POST | Send data |
| PUT | Update data |
| DELETE | Remove data |
-
JSON (mostly)
-
Not HTML pages
from flask import Flask, jsonify, requestfrom flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/hello', methods=['GET'])
def hello_api():
return jsonify({
"message": "Hello, this is a GET API",
"status": "success"
})
if __name__ == '__main__':
app.run(debug=True)http://127.0.0.1:5000/api/hello
@app.route('/api/greet/<name>', methods=['GET'])
def greet(name):
return jsonify({
"greeting": f"Hello {name}"
})Example:
/api/greet/John
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/add', methods=['POST'])
def add_numbers():
data = request.get_json()
a = data.get('a')
b = data.get('b')
return jsonify({
"sum": a + b
})
if __name__ == '__main__':
app.run(debug=True){
"a": 10,
"b": 20
}from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/add', methods=['GET'])
def add_numbers():
a = request.args.get('a')
b = request.args.get('b')
return jsonify({
"a": a,
"b": b
})
if __name__ == '__main__':
app.run(debug=True)http://127.0.0.1:5000/add?a=10&b=20-
Reads data sent by client
-
Used for:
-
JSON
-
Form data
-
Query parameters
-
-
Converts Python dictionary → JSON
-
Automatically sets response headers
from flask import Flask, jsonify, request
app = Flask(__name__)
students = []
@app.route('/students', methods=['POST'])
def add_student():
data = request.get_json()
students.append(data)
return jsonify({"message": "Student added"})
@app.route('/students', methods=['GET'])
def get_students():
return jsonify(students)
if __name__ == '__main__':
app.run(debug=True)| Concept | Meaning |
|---|---|
| Route | URL mapping |
| View Function | Function handling request |
| Request | Data from client |
| Response | Data sent back |
| JSON | Data format for APIs |
| Debug Mode | Helps during development |
Use Flask when:
-
Teaching web development
-
Building REST APIs
-
Creating microservices
-
Prototyping quickly