If you have worked around Python and Web technologies then you have heard of Flask. It is well known and there are a lot of examples for getting started with simple apps.
Simple/single apps are great if you are learning, but quickly bog down if you have even a little bit of complexity.
Here are some key things to know before you get too far into a monolithic application.
Modules
Blueprints is the Flask concept for bringing in sub-things or modules. The official documentation is clear and provides an example for rendering a page, but similar concept applies for APIs if your application has no web content.
Our recommendation is, as soon as you have the simple app understood, break it into modules (in your preferred pattern) before adding any more routes or pages.
Exceptions
If you leave the exception handling up to Flask, especially if you have an API only application, you will end up with the base errors being returned, like Method Not Allowed or Server Error and the calling client will have no idea what the cause may be.
To deal with this, you can use exception handlers. Create a MyApplicationException and when that exception occurs, return a json object with meaningful description which your client can then understand. In this Handling Application Errors example, instead of catching HTTPException, catch MyApplicationException, and adjust so that response = HTTPException().get_response() to setup the base exception to send onwards by returning response, e.code.
from flask import json
from werkzeug.exceptions import HTTPException
@app.errorhandler(MyApplicationException)
def handle_exception(e):
"""Return JSON instead of HTML for our application errors."""
# start with an empty http exception as that is what we wish to return
response = HTTPException().get_response()
# replace the body with JSON
response.data = json.dumps({
"code": e.code,
"description": e.description,
#any other key values you want the client to have access to
})
response.content_type = "application/json"
return response, e.code