-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
53 lines (45 loc) · 1.68 KB
/
index.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
import json
import calculator as calc
import str_parser as parser
from flask import Flask, render_template, request
from flask_cors import CORS, cross_origin
app = Flask(__name__, static_url_path="/")
cors = CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
@app.route("/")
@cross_origin()
def index():
return app.send_static_file("index.html")
@app.route("/calculate", methods=["POST"])
@cross_origin()
def calculate():
"""
Calculates the tangent equation of a function at a given point.
"""
data = request.get_json()
try: # Load the request data
fcn = str(data["fcn"])
x = str(data["x"])
y = str(data["y"])
output = str(data["output"])
except (KeyError, TypeError):
return json.dumps({"error": "Error accessing data from request."}), 400
# Validate the data
if fcn is None or x is None or y is None or output not in ["exact", "decimal"]:
return json.dumps({"error": "Invalid request data"}), 400
# Parse data to sympy expressions
try:
if "y" not in fcn:
fcn = f"y - ({fcn})" # The dependent variable, y, is required.
fcn = parser.parse(fcn)
x = parser.parse(x)
y = parser.parse(y)
except Exception as e:
return json.dumps({"error": f"Error parsing data: {e}"}), 400
# Calculate the tangent equation
dy_dx, lines = calc.calculate(fcn, x, y, output == "exact")
if isinstance(lines, Exception):
return json.dumps({"error": f"Error calculating tangent equation: {lines}", "dy_dx": str(dy_dx)}), 500
return json.dumps({"dy_dx": str(dy_dx), "lines": [vars(line) for line in lines]}), 200
if __name__ == "__main__":
app.run(threaded=True)