-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
102 lines (83 loc) · 3.77 KB
/
app.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
import os, signal, math
from flask import Flask, render_template, flash, request
from tools import tempconversion
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField, SelectField
DEBUG = True
app = Flask(__name__)
app.secret_key = b'?]\A_??l?s??de?'
TEMP_UNITS = [('Kelvin', 'Kelvin'), ('Celsius', 'Celsius'), ('Fahrenheit', 'Fahrenheit'), ('Rankine', 'Rankine')]
class MyForm(Form):
input_temp = TextField('Input Temperature', validators=[validators.required()])
input_unit = SelectField('Input Unit', choices=TEMP_UNITS, validators=[validators.required()])
target_unit = SelectField('Target Unit', choices=TEMP_UNITS, validators=[validators.required()])
student_response = TextField('Student Response', validators=[validators.required()])
signal.signal(signal.SIGINT, lambda s, f: os._exit(0))
def check_student_answer(input_temp, input_unit, target_unit, student_response):
if input_unit == 'Celsius':
if target_unit == 'Fahrenheit':
converted_value=tempconversion.celsius_to_fahrenheit(input_temp)
elif target_unit == 'Kelvin':
converted_value=tempconversion.celsius_to_kelvin(input_temp)
elif target_unit == 'Rankine':
converted_value=tempconversion.celsius_to_rankine(input_temp)
elif input_unit == 'Fahrenheit':
if target_unit == 'Celsius':
converted_value=tempconversion.fahrenheit_to_celsius(input_temp)
elif target_unit == 'Kelvin':
converted_value=tempconversion.fahrenheit_to_kelvin(input_temp)
elif target_unit == 'Rankine':
converted_value=tempconversion.fahrenheit_to_rankine(input_temp)
elif input_unit == 'Kelvin':
if target_unit == 'Celsius':
converted_value=tempconversion.kelvin_to_celsius(input_temp)
elif target_unit == 'Fahrenheit':
converted_value=tempconversion.kelvin_to_fahrenheit(input_temp)
elif target_unit == 'Rankine':
converted_value=tempconversion.kelvin_to_rankine(input_temp)
elif input_unit == 'Rankine':
if target_unit == 'Celsius':
converted_value=tempconversion.rankine_to_celsius(input_temp)
elif target_unit == 'Fahrenheit':
converted_value=tempconversion.rankine_to_fahrenheit(input_temp)
elif target_unit == 'Kelvin':
converted_value=tempconversion.rankine_to_kelvin(input_temp)
else:
converted_value=None
msg = 'The student response is '
msg += 'correct.' if converted_value == student_response else ("incorrect. The correct answer is " + str(converted_value))
return msg
def is_float(input):
try:
assert float(input)
except:
return False
return True
def is_int(input):
try:
assert int(input)
except:
return False
return True
def is_numeric(input):
if is_float(input) or is_int(input):
return True
else:
return False
@app.route("/", methods=['GET', 'POST'])
def temp():
form = MyForm(request.form)
if request.method == 'POST':
if form.validate():
if request.form['input_unit'] == request.form['target_unit']:
flash("Input Unit and Target Unit must be different type")
elif not is_numeric(request.form['input_temp']):
flash("Input Temperature invalid.")
elif not is_numeric(request.form['student_response']):
flash("The student response is incorrect.")
else:
flash(check_student_answer(float(request.form['input_temp']), request.form['input_unit'], request.form['target_unit'], float(request.form['student_response'])))
else:
flash('All fields required.')
return render_template('temperature.html', form=form)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(os.getenv('PORT', 5000)))