-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
144 lines (105 loc) · 4.23 KB
/
app.py
File metadata and controls
144 lines (105 loc) · 4.23 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import chemica.engine as chemica
from chemica.substance import Substance
from chemica.structure import Structure
from chempy import chemistry
from helpers import *
import PIL
from flask import Flask, render_template, request, redirect
import sys
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/", methods=["GET", "POST"])
def index():
""" Simple Reaction Calculator """
if request.method == "POST":
A_reagent = request.form.get("A")
B_reagent = request.form.get("B")
# Make sure there is input
if A_reagent is None or B_reagent is None:
return apology("Missing Reagent", 401)
# Solve for products
try:
solved = chemica.solve(A_reagent, B_reagent)
except Exception:
return apology("Invalid reagents", 401)
# If there is an error
if type(solved) == str:
return apology(solved, 401)
# Get the balanced equation and the reagent names by unpacking tuple
# Why? balanced_eq has coef will reagents don't (except for dict values)
balanced_equation, reagents = solved
# Parse balanced equation
# Get the fixed reagents
fixed_reagents = balanced_equation.split("→")
# Reactants
fixed_A, fixed_B = fixed_reagents[0].split("+")
# Products
fixed_products = fixed_reagents[1]
# Parse reagent names
reactants, products = reagents
reac_reagents = list(reactants.keys())
prod_reagents = list(products.keys())
# If it has more than 1 product, forcibly get an error 'message'
if len(prod_reagents) > 1:
prod_reagents = ["C4"]
return render_template("predicted.html", A=fixed_A, B=fixed_B, prod=fixed_products, reac_reagents=reac_reagents, prod_reagents=prod_reagents)
return render_template("predictor.html")
@app.route("/lewis", methods=["GET", "POST"])
def lewis():
""" Generate Lewis Structure Procedurally """
if request.method == "POST":
compound = request.form.get("compound")
# Make sure there is input
if compound is None:
return apology("Missing Compound", 401)
return render_template("lewis.html", compound=compound)
return render_template("lewis.html")
@app.route("/name", methods=["GET", "POST"])
def name():
""" Name Compound Procedurally """
if request.method == "POST":
compound = request.form.get("compound")
# Make sure there is input
if compound is None:
return apology("Missing Compound", 401)
# Name compound
try:
compound_name = Substance.from_formula(compound).nomenclature
except Exception:
return apology("Invalid reagents", 401)
return render_template("name.html", compound_name=compound_name)
return render_template("name.html")
@app.route("/balance", methods=["GET", "POST"])
def balance():
""" Name Compound Procedurally """
if request.method == "POST":
reactants = request.form.get("reactants")
products = request.form.get("products")
# Make sure there is input
if reactants is None:
return apology("Missing Reactants", 401)
if products is None:
return apology("Missing Products", 401)
# Parse reagents
reactants = reactants.split("+")
products = products.split("+")
# Balance reaction
try:
reac, prod = chemistry.balance_stoichiometry(reactants, products)
except Exception:
return apology("Invalid reagents", 401)
balanced_equation = chemica.reaction_equation(reac, prod)
return render_template("balance.html", balanced_equation=balanced_equation)
return render_template("balance.html")
@app.route("/lewis_api/<formula>")
def lewis_api(formula):
structure = Structure.from_formula(formula)
return serve_pil_image(structure.lewis)