Browse Source

getting data returned from database, found neat option/trick in postgres using row_as_json(tablename) in select statement. Returning the json rows throws an error, but using jsonify makes results return a row inside a row. Need to figure that out.

kc-covid19-zipcode
prasket 2 months ago
parent
commit
ed5e157e03
2 changed files with 12 additions and 3 deletions
  1. +2
    -1
      .gitignore
  2. +10
    -2
      api.py

+ 2
- 1
.gitignore View File

@@ -1 +1,2 @@
.vscode
.vscode
test.py

+ 10
- 2
api.py View File

@@ -1,5 +1,7 @@
import flask
from flask import request, jsonify
import psycopg2
import json

app = flask.Flask(__name__)
app.config["DEBUG"] = True
@@ -53,14 +55,20 @@ zipcodes = [

@app.route('/', methods=['GET'])
def home():
return "<h1>Public API offered by the team at SeattleMatrix.</h1> <p>This \
return "<h1>Public API offered by the team at Seattle Matrix.</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)
conn = psycopg2.connect("host='localhost' dbname='apiv1' user='postgres' password='postgres'" )
cur = conn.cursor()
cur.execute("select row_to_json(king_county_covid19_zip) from king_county_covid19_zip;")
rv = cur.fetchall()
# returning rv throws an error and jsonify returns double nested
# json result need to figure this out.
return jsonify(rv)

@app.route('/v1/kingcounty/covid-19/zipcode/', methods=['GET'])
def api_zipcode():


Loading…
Cancel
Save