In today's lab, we will continue to learn how to call and read API data to our Flask apps.
Define a list of values
Implement loops to iterate through an array in Python
Implement conditionals that checks a value against each element of the list
Use operators to compare values
Use the curl command to save data to the correct directory
Import multiple items from flask
Write new routes with API endpoints to handle GET requests
Open JSON data using the with keyword and open function
Return JSON data with jsonify
In Python, lists are data structures that are used to store sets of values. Lists are writen with brackets [ ] and each value is comma separated.
>>> contacts = ["Janelle", "Kali", "Bob", "Tatiana"]
>>>len(contacts)
5
Loops can be used to iterate through sets of data. It will access each element in a data set and you can use them in combination with conditional statements.
>>> bob = "Bob"
>>> for c in contacts:
... if c == bob:
... print("Bob is a contact")
... else:
... print("No match")
Another way to write them in combination is:
>>> if "Bob" in contacts:
... print("Bob is a contact")
Bob is a contact
The data in the JSON objects are stored as key/value pairs. In the example on the right, the keys are the items on the left of the ( : ), such as business_id, address, and city. The values are the data listed to the right of the ( : ) are the values.
Note that some values can be objects themselves. In the example, the attributes key has a value of an object that contains attributes of the restaurant.
{ "business_id": "tnhfDv5Il",
"name": "Garaje",
"address": "475 3rd St",
"city": "San Francisco",
"state": "CA",
"postal code": "94107",
"latitude": 37.7817529521,
"longitude": -122.39612197,
"stars": 4.5,
"review_count": 1198,
"attributes": {
"RestaurantsTakeOut": true,
"BusinessParking": {
"garage": false,
"street": true,
"validated": false,
"lot": false,
"valet": false
},
}
}
import os
from flask import Flask, render_template, request, json, jsonify, current_app as app
app = Flask(__name__)
albums_path = os.path.join(app.static_folder, 'data', 'albums.json')
@app.route('/api/v1/albums/all', methods=['GET'])
def api_albums_all():
with open(albums_path, 'r') as jsondata:
albums = json.load(jsondata)
return jsonify(albums)
Importing the OS module in python allows us to interact with the file system. Here, we need to access the JSON data that is stored in the static folder.
request
Imports the request library and object to send external HTTP GET requests to grab the specific user-provided URL, while the latter is used to handle GET and POST requests within the Flask app
json
Allows programmers to utilize JSON objects in Python code
The OS module provides us with a sub module: os.path which is used for common pathname manipulation. Here, it will concatenate the various path components with a directory separator ('/')
This function takes in a path argument and an HTTP method. Here, we pass in the path to all of the album data and the GET method.
The with keyword will automatically close the file that we are working with after we are done with it.
The open function will take a file and the mode we are opening the file. We can open the file in read ('r'), write ('w'), append ('a'), or create ('x') mode.
This function turns the JSON data into a Response object that you can work with.