Add a function to generate JSON from the DB

This commit is contained in:
William Tran 2021-03-15 17:33:18 -04:00 committed by Neil Parikh
parent 6a3c29da66
commit 3025ffce5c
1 changed files with 21 additions and 0 deletions

View File

@ -1,9 +1,30 @@
from flask import Flask
app = Flask(__name__)
import json
import sqlite3
import os
DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db')
@app.route('/')
def hello_world():
return 'Hello, World!'
def regen_JSON():
"""Gets links from DB and outputs them in JSON"""
con = sqlite3.connect(DB_PATH)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute('SELECT url, name FROM links ORDER BY position')
links_list = []
for row in cur.fetchall():
d = dict(zip(row.keys(), row))
links_list.append(d)
links_json = json.dumps(links_list, indent=4)
con.close()
return links_json
if __name__ == "__main__":
app.run(debug=True)