April 2018
Beginner
340 pages
7h 54m
English
Let's modify our simple example to return JSON instead of a web page:
from flask import Flask, jsonify...@app.route("/")def index(): data = { "cats": 5, "people": 8, "dogs": 4 } return jsonify(data) ...
Flask comes with a module called jsonify, which translates a dictionary into JSON and returns it as the response. This makes handling JSON data very easy.
In this example, we create a dictionary with some example values. In a proper application, this could instead be data which has been pulled from a database and manipulated in some way.
This data is then passed to the jsonify function and given to the requesting application in JSON format.
Make sure your web server is running (run python3 server.py if it isn't), then open up a ...