How to return a dict as a JSON response from a Flask view?

As of Flask 1.1.0 a view can directly return a Python dict and Flask will call jsonify automatically. @app.route(“/summary”) def summary(): d = make_summary() return d If your Flask version is less than 1.1.0 or to return a different JSON-serializable object, import and use jsonify. from flask import jsonify @app.route(“/summary”) def summary(): d = make_summary() … Read more

How to serve static files in Flask

In production, configure the HTTP server (Nginx, Apache, etc.) in front of your application to serve requests to /static from the static folder. A dedicated web server is very good at serving static files efficiently, although you probably won’t notice a difference compared to Flask at low volumes. Flask automatically creates a /static/<path:filename> route that … Read more

Get the data received in a Flask request

The docs describe the attributes available on the request object (from flask import request) during a request. In most common cases request.data will be empty because it’s used as a fallback: request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle. request.args: the key/value pairs in … Read more