Flask web framework¶
Flask is a popular web framework for Python used to build HTTP applications^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md]. It allows developers to define web routes, handle various HTTP methods, and manage external dependencies using utilities like pip^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
Basic Application¶
A minimal Flask application requires creating an application instance and defining route functions that return content^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
To run the application, the environment variable FLASK_APP is set to the application entry point, followed by the run command^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
export FLASK_APP=src/app
flask run -h 0.0.0 -p 5000
Dependencies and Installation¶
Flask is classified as an external package, managed separately from Python's built-in libraries^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md]. Dependencies are typically listed in a requirements.txt file to ensure consistent environments^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
Flask == 2.0.2
Installation is performed using pip^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md]:
pip install -r src/requirements.txt
Routing and HTTP methods¶
Flask uses decorators to bind functions to specific URL routes^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md]. Routes can be configured to accept specific HTTP methods, such as GET for data retrieval or POST for submitting data^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
Variable rules can be applied to routes to capture dynamic components of the URL^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
@app.route("/get/<string:customerID>", methods=['GET'])
def get_customer(customerID):
return "<p>Hello, get!</p>"
Request Handling¶
Processing incoming data, typically in JSON format, requires importing the request object^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md]. To correctly parse the request body as JSON, the client must include the Content-Type: application/json header^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
from flask import request
@app.route("/add", methods=['POST'])
def add_customer():
print(request.json)
return "success", 200
Status Codes¶
Flask allows functions to return specific HTTP status codes alongside the response data^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
- 200 OK: Indicates a successful request^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
- 404 Not Found: Used when a requested resource, such as a specific customer ID, does not exist^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
- 400 Bad Request: Returned when the client sends malformed data, such as invalid JSON^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
- 500 Internal Server Error: Indicates an unhandled exception within the application code^[400-devops-09-scripting-language-python-introduction-part-4http-readme.md].
Related Concepts¶
- [[Python]]
- [[REST API]]
- [[HTTP]]
- [[Docker]]
Sources¶
400-devops-09-scripting-language-python-introduction-part-4http-readme.md