-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.wsgi
executable file
·68 lines (56 loc) · 2.04 KB
/
board.wsgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python3
from flask import Flask, request, jsonify, render_template
from flask_restful import Resource, Api
import sqlalchemy as db
import json
import uptime
import datetime
engine = db.create_engine('sqlite:///var/www/html/python/colour.db')
meta_data = db.MetaData()
connection = engine.connect()
create_table = db.Table('colour', meta_data,
db.Column('id',db.Integer, primary_key=True),
db.Column('background',db.String(255))
)
meta_data.create_all(engine)
query = connection.execute("SELECT background FROM colour WHERE id = 1")
row = query.cursor.fetchone()
if row == None:
push = db.insert(create_table).values(id=1,background='green')
result = connection.execute(push)
application = Flask(__name__)
api = Api(application)
class Background(Resource):
def get(self):
conn = engine.connect()
query = conn.execute("SELECT background FROM colour WHERE id = 1")
return { 'background': i[0] for i in query.cursor.fetchall()}
def post(self):
conn = engine.connect()
print(request.json)
background = request.json['background']
query = conn.execute("UPDATE colour SET background = '{0}' WHERE id = 1".format(background))
return { 'status': 'success'}
class Uptime(Resource):
def get(self):
up = uptime.uptime()
readable_up = str(datetime.timedelta(seconds=round(up)))
return jsonify(up = readable_up)
class Boot(Resource):
def get(self):
boot = uptime.boottime()
return jsonify(boot = boot)
api.add_resource(Background, '/background')
api.add_resource(Uptime, '/uptime')
api.add_resource(Boot, '/boot')
@application.route('/')
def index():
conn = engine.connect()
query = conn.execute("SELECT * FROM colour ORDER BY id DESC LIMIT 1")
bgc = query.cursor.fetchall()
for colour in bgc:
c = colour[1]
print(c)
return render_template('index.html', background=c)
if __name__ == "__main__":
application.run()