Reputation: 7
I am working with a flask app that queries my sqlite db and pulls out info on number of disease cases in each state in each year. The route below pulls the correct data, but for some reason, will print not out the strings such as "Cases_2010", "Rate_2010, etc" that are in the request. Is this is problem with the string itself or the concatenation? It does print the "State" string, just not any after that.
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, desc
from flask import Flask, jsonify
import json
import numpy as np
import os
engine = create_engine("sqlite:///westnile_1.sqlite")
Base = automap_base()
Base.prepare(engine, reflect=True)
Disease_Data = Base.classes.disease_data
session = Session(engine)
app = Flask(__name__)
@app.route("/")
def home():
return(
f"Welcome to the West Nile Virus Case Report API <br>"
f"Available routes: <br>"
f"/api/v1.0/Cases_per_year_2010_2017 <br>"
f"/api/v1.0/case_totals <br>"
f"/api/v1.0/json_combined <br>")
@app.route("/api/v1.0/Cases_per_year_2010_2017")
def cases_2010_2017():
results_10 = session.query("State: " + Disease_Data.State, "Cases_2010: " + Disease_Data.Cases_2010, "Rate_2010: " + Disease_Data.Rate_2010,
"Cases_2011: " + Disease_Data.Cases_2011, "Rate_2011: " + Disease_Data.Rate_2011,
"Cases_2012: " + Disease_Data.Cases_2012, "Rate_2012: " + Disease_Data.Rate_2012,
"Cases_2013: " + Disease_Data.Cases_2013, "Rate_2013: " + Disease_Data.Rate_2013,
"Cases_2014: " + Disease_Data.Cases_2014, "Rate_2014: " + Disease_Data.Rate_2014,
"Cases_2015: " + Disease_Data.Cases_2015, "Rate_2015: " + Disease_Data.Rate_2015,
"Cases_2016: " + Disease_Data.Cases_2016, "Rate_2016: " + Disease_Data.Rate_2016,
"Cases_2017: " + Disease_Data.Cases_2017, "Rate_2017: " + Disease_Data.Rate_2017).all()
json2010 = json.dumps(results_10)
return jsonify(results_10)
updated output of return jsonify (json2010)
Upvotes: 0
Views: 909
Reputation: 8273
As I don't have any info on ORM hence I am assuming Orm has the exact number of columns as the table has.
results_10 = session.query(Disease_Data).all() # This will return the list of Disease_Data objects
json_list= []
for result in results_10:
temp_dict = result.__dict__
temp_dict.pop('_sa_instance_state',None)
json_list.append(temp_dict)
# OR
# temp_dict = dict(result)
# temp_dict.pop('_sa_instance_state',None) # removing unwanted key
# json_list.append(dict(result)) #Either of two should work
json2010 = json.dumps(json_list)
return jsonify(results_10)
I could not test it on my local but should give an idea.
In case you want to keep the format of the data:
column_list = Disease_Data.__table__.columns.keys()
results_10 = session.query(Disease_Data).all() # This will return the list of Disease_Data objects
json_list= []
for result in results_10:
temp_dict={}
for col in column_list:
temp_dict[col] = getattr(result,col)
json_list.append(temp_dict)
json2010 = json.dumps(json_list)
return jsonify(results_10)
Upvotes: 1