File size: 1,245 Bytes
192a1c0
31806c5
6377159
31806c5
 
 
 
 
 
 
 
e92ba0d
6f847ac
 
 
 
 
 
e92ba0d
 
30dd301
e92ba0d
 
 
24f2542
 
192a1c0
6f847ac
f04bbb4
 
 
6377159
f04bbb4
 
 
 
6377159
 
f04bbb4
 
 
6377159
ba1e2db
6377159
 
ba1e2db
 
6377159
 
 
c2ccf60
31806c5
3563daa
1936952
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
from flask import Flask, request, jsonify, make_response, render_template

from dataset.iris import iris
from opts import options

app = Flask(
    __name__,
    template_folder="templates",
)


def not_valid(params: dict):
    """
    is_valid() simply checks if
    an incoming response is valid
    for this api or not. Key to
    avoiding errors
    """
    if "algorithm" not in params:
        return "User did not specify the algorithm parameter"

    if params["algorithm"] not in options:
        return f"Invalid algorithm '{params['algorithm']}' is invalid."
    return False


@app.route("/", methods=["POST", "GET"])
def index():
    if request.method == "GET":
        return render_template("index.html")

    error_message = not_valid(params=request.json)
    if error_message:
        return make_response(error_message, 400)

    # parse arguments
    algorithm = options[request.json["algorithm"]]
    args = request.json["arguments"]

    # in the future instead of a random data set
    # we should do a more real one like palmer penguins

    X, y = iris()
    return jsonify(
        algorithm(
            X=X,
            y=y,
            args=args,
        )
    )


if __name__ == "__main__":
    app.run(debug=False)