O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using JSON

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required