-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
71 lines (58 loc) · 2.3 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
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
# -*- coding: utf-8 -*-
"""
Flask module for running the TruthTable module app
"""
from __future__ import unicode_literals
from flask import Flask, Markup, render_template, request
import truthtables
TRUE_STRING = "<span style='color: green;'>T</span>"
FALSE_STRING = "<span style='color: red;'>F</span>"
FLASK_APP = Flask(__name__)
@FLASK_APP.route("/")
def index_page():
return render_template('index.html')
@FLASK_APP.route("/submit", methods=['POST'])
def generate_table():
formulas = request.form.getlist('formula[]')
i = 0
while i < len(formulas):
formulas[i] = str(formulas[i]).strip()
if len(formulas[i]) == 0:
del formulas[i]
i -= 1
i += 1
form = Markup(render_template('form.html', formulas=formulas))
try:
table = truthtables.runner(formulas)
except (SyntaxError, TypeError) as exception:
return render_template('error.html', error=str(exception), form=form)
pretty = [truthtables.pretty_print(formula) for formula in table.formulas]
rows = []
for i in range(len(table.combinations)):
rows.append([])
for j in range(len(table.formulas)):
temp = pretty[j]
new_str = u""
count = 0
for k in range(len(temp)):
if not truthtables.is_connective(temp[k]):
new_str += " "
else:
if table.main_connective_index[j][1] == count:
new_str += "<b><u>"
if table.connective_evaluation[i][j][count]:
new_str += TRUE_STRING
else:
new_str += FALSE_STRING
if table.main_connective_index[j][1] == count:
new_str += "</b></u>"
count += 1
if len(new_str.replace(" ", "").strip()) == 0:
new_str = "<b><u>" + (TRUE_STRING if table.evaluation[i][j] else
FALSE_STRING) + "</u></b>"
rows[-1].append(Markup(new_str))
return render_template('table.html', formulas=pretty, table=table, rows=rows, form=form,
table_statements=table.get_table_assessment())
if __name__ == '__main__':
FLASK_APP.debug = True
FLASK_APP.run()