Skip to content

Instantly share code, notes, and snippets.

@TheSithPadawan
Last active May 29, 2018 23:36
Show Gist options
  • Save TheSithPadawan/d3089ae93d0bd3b17e5e92c7582b129e to your computer and use it in GitHub Desktop.
Save TheSithPadawan/d3089ae93d0bd3b17e5e92c7582b129e to your computer and use it in GitHub Desktop.
'''
$ python app.py
put this in your browser: http:127.0.0.1:5000 to view the json
'''
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template
import json
app = Flask(__name__)
app.config.update(
SECRET_KEY = 'Florence',
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:Florence@localhost/gre_db',
SQLALCHEMY_TRACK_MODIFICATIONS=False
)
db = SQLAlchemy(app)
@app.route('/')
def get_data():
query = db.session.query(Question).all()
output = []
for row in query:
output.append({"question":row.text, "options":row.choices})
return json.dumps(output)
class Question(db.Model):
__tablename__ = 'question'
id = db.Column(db.Integer, primary_key=True, unique = True)
text = db.Column(db.String(5000), nullable=False)
choices = db.Column(db.String(5000), nullable=False)
def __init__(self, text, choices, qid):
self.text = text
self.choices = choices
self.id = qid
def __repr__(self): # for debug purpose
return self.text + '\n' + self.choices
class Answer(db.Model):
__tablename__ = 'answer'
id = db.Column(db.Integer, primary_key=True)
answers = db.Column(db.String(100), nullable=False)
# relationship
question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
def __init__(self, ans, qid):
self.answers = ans
self.question_id = qid
def __repr__(self):
return 'answer to question id {} is {}'.format(self.question_id, self.answers)
if __name__ == '__main__':
db.create_all()
app.run(debug = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment