forked from arunvalluthadam/paintApp_Html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
39 lines (31 loc) · 1.03 KB
/
server.py
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
from flask import Flask, render_template, request, g, redirect
import sqlite3
import psycopg2
import json
app = Flask(__name__)
def connect_db():
return sqlite3.connect('jsondata.db')
@app.before_request
def before_request():
g.db = connect_db()
g.db.execute("CREATE TABLE IF NOT EXISTS drawings(fname string primary key, img_data text)")
@app.after_request
def after_request(response):
g.db.close()
return response
@app.route('/', methods=['GET', 'POST'])
def paint():
if request.method == 'GET':
py_all = {}
all_data = g.db.execute("SELECT * FROM drawings")
for data in all_data:
py_all[data[0]] = data[1]
return render_template('index.html', py_all= py_all)
elif request.method == 'POST':
filename = request.form['fname']
data = request.form['whole_data']
g.db.execute("REPLACE INTO drawings(fname, img_data) VALUES (?, ?)", (filename, data));
g.db.commit()
return redirect('/')
if __name__ == '__main__':
app.run(debug = True)