Add a function to generate JSON from the DB
This commit is contained in:
parent
6a3c29da66
commit
3025ffce5c
|
@ -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)
|
Loading…
Reference in New Issue