-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
91 lines (69 loc) · 2.11 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
import base64
from io import BytesIO
import matplotlib
from flask import Flask, redirect, render_template, request, url_for
from sympy import Symbol, diff, simplify
from sympy.plotting import PlotGrid, plot
matplotlib.use("Agg")
app = Flask(__name__)
@app.route("/")
def main():
return render_template("base.html")
@app.route("/submit", methods=["GET", "POST"])
def submit():
if request.method == "POST":
user_inp = request.form.get("input")
user_inp = str(user_inp).replace("**", "^")
return redirect(url_for("input", expr=user_inp))
@app.route("/input/expr=<path:expr>")
def input(expr):
try:
var = Symbol("x")
try:
deriv = simplify(diff(expr, var))
deriv = str(deriv).replace("**", "^")
except (MemoryError, TypeError, ValueError) as error:
return render_template(
"index.html",
func_error=f"{expr} (Invalid input!)",
deriv_error=f"{error} (Error occurred!)",
)
p1 = plot(
deriv,
(var, -3, 3),
xlabel="x",
ylabel="y",
title="(x from -3 to 3)",
show=False,
)
p2 = plot(
deriv,
(var, -20, 20),
xlabel="x",
ylabel="y",
title="(x from -20 to 20)",
show=False,
)
fig = PlotGrid(2, 1, p1, p2, show=False)
buf = BytesIO()
fig.save(buf)
data = base64.b64encode(buf.getbuffer()).decode("ascii")
except (IndexError, ValueError) as error:
return render_template(
"index.html",
func=f"`{expr}`",
deriv=f"`d/dx({expr}) = {deriv}`",
plot_error=f"{error} (Error occurred!)",
)
else:
return render_template(
"index.html",
func=f"`{expr}`",
deriv=f"`d/dx({expr}) = {deriv}`",
plot=f"data:image/png;base64,{data}",
)
@app.errorhandler(Exception)
def exception_handler(error):
return str(error) + "!!"
if __name__ == "__main__":
app.run(debug=False)