Cheat Sheet: Web App Deployment Using Flask
Flask
Web
Quick reference for building simple Flask endpoints and handling HTTP status codes.

Estimated time needed: 5 minutes
Flask
Used to instantiate an object of the Flask class named app.
from flask import Flask
app = Flask(__name__)@app.route decorator
A decorator in Flask used to map URLs to specific functions in a Flask application.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "My first Flask application in action!"200 OK status
Flask returns a 200 OK status by default when a route returns a value. You can also return it explicitly.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
# Implicit 200 OK
return "My first Flask application in action!"
@app.route('/explicit')
def explicit_ok():
# Explicit 200 OK
return "My first Flask application in action!", 200Error statuses
- 400: Invalid request (e.g., missing or improper parameters).
- 401: Missing or invalid credentials.
- 403: Insufficient permissions to fulfill the request.
- 404: Resource not found.
- 405: Method not allowed.
Example returning validation and not-found errors:
from flask import Flask, request
app = Flask(__name__)
@app.route('/search')
def search_response():
query = request.args.get("q")
if not query:
return {"error_message": "Input parameter missing"}, 422
# fetch the resource from the database
resource = fetch_from_database(query) # implement this function
if resource:
return {"message": resource}
else:
return {"error_message": "Resource not found"}, 404Error 500
500 is used when there is an error on the server.
from flask import Flask
app = Flask(__name__)
@app.errorhandler(500)
def server_error(error):
return {"message": "Something went wrong on the server"}, 500