-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunit5_webapp.py
106 lines (83 loc) · 2.79 KB
/
unit5_webapp.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask, render_template, redirect, request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import statistics
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///formdata.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 'True'
db = SQLAlchemy(app)
class Formdata(db.Model):
__tablename__ = 'formdata'
id = db.Column(db.Integer, primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.now)
firstname = db.Column(db.String, nullable=False)
email = db.Column(db.String)
age = db.Column(db.Integer)
income = db.Column(db.Integer)
satisfaction = db.Column(db.Integer)
q1 = db.Column(db.Integer)
q2 = db.Column(db.Integer)
def __init__(self, firstname, email, age, income, satisfaction, q1, q2):
self.firstname = firstname
self.email = email
self.age = age
self.income = income
self.satisfaction = satisfaction
self.q1 = q1
self.q2 = q2
db.create_all()
@app.route("/")
def welcome():
return render_template('welcome.html')
@app.route("/form")
def show_form():
return render_template('form.html')
@app.route("/raw")
def show_raw():
fd = db.session.query(Formdata).all()
return render_template('raw.html', formdata=fd)
@app.route("/result")
def show_result():
fd_list = db.session.query(Formdata).all()
# Some simple statistics for sample questions
satisfaction = []
q1 = []
q2 = []
for el in fd_list:
satisfaction.append(int(el.satisfaction))
q1.append(int(el.q1))
q2.append(int(el.q2))
if len(satisfaction) > 0:
mean_satisfaction = statistics.mean(satisfaction)
else:
mean_satisfaction = 0
if len(q1) > 0:
mean_q1 = statistics.mean(q1)
else:
mean_q1 = 0
if len(q2) > 0:
mean_q2 = statistics.mean(q2)
else:
mean_q2 = 0
# Prepare data for google charts
data = [['Satisfaction', mean_satisfaction], ['Python skill', mean_q1], ['Flask skill', mean_q2]]
return render_template('result.html', data=data)
@app.route("/save", methods=['POST'])
def save():
# Get data from FORM
firstname = request.form['firstname']
email = request.form['email']
age = request.form['age']
income = request.form['income']
satisfaction = request.form['satisfaction']
q1 = request.form['q1']
q2 = request.form['q2']
# Save the data
fd = Formdata(firstname, email, age, income, satisfaction, q1, q2)
db.session.add(fd)
db.session.commit()
return redirect('/')
if __name__ == "__main__":
app.debug = True
app.run()