API to help make public data easier to access. Idea started with need for King County, WA COVID-19 data in API format while only having an excel file for source data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# Create some test data this will eventually be in a Postgresql db.
zipcodes = [
{'id': 0,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98121',
'population': '22274',
'tests': '3182',
'test_rate': '142.86',
'positives': '64',
'positive_rate': '2.87',
'hospitalizations': '4',
'hospitalization_rate': '0.18',
'deaths INTEGER': '2',
{'id': 1,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98109',
'population': '34939',
'tests': '6142',
'test_rate': '175.79',
'positives': '206',
'positive_rate': '5.9',
'hospitalizations': '38',
'hospitalization_rate': '1.09',
'deaths INTEGER': '19',
{'id': 2,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98116',
'population': '26979',
'tests': '5259',
'test_rate': '194.93',
'positives': '121',
'positive_rate': '4.48',
'hospitalizations': '5',
'hospitalization_rate': '0.19',
'deaths INTEGER': '2',
@app.route('/', methods=['GET'])
def home():
return "<h1>Public API offered by the team at SeattleMatrix.</h1> <p>This \
site is a collection of api's from various public sources collected \
by the team at Seattle Matrix</p>"
# A route to return all of the available entries in our catalog.
@app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
def api_all():
return jsonify(zipcodes)
@app.route('/v1/kingcounty/covid-19/zipcode/', methods=['GET'])
def api_zipcode():
# Check to see if 'zipcode' was provided in URL
# If no zipcode display message stating this
if 'zipcode' in request.args:
provided_zipcode = request.args['zipcode']
return "Error: No zipcode field provided. Please specify an zipcode."
# Create an empty list for our results
results = []
# Loop through the data and match results that fit the requested Zipcode.
# zipcodes are unique, but other fields might return many results
for zipcode in zipcodes:
if zipcode['zipcode'] == provided_zipcode:
# If no zipcodes matched and results are empty show message rather than
# empty page, maybe this isnt good to do in an API that is going to be used
# for human and computer consumption, need to check into that.
if not results:
results = "The Zipcode you entered was not in the API, please try again"
# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)