0

I have already finished my function, but when I run the webserver I get the following error:

TypeError : index() missing 1 required positional argument:'input'

This is my code:

from sklearn import tree
from flask import Flask

app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def index(input):
    input = [[1,1,2,3,3,1,1,2]]

    data = pd.read_csv('datawadek.csv')
    y = data.KELAS
    x = data.drop('KELAS', axis = 1)

    cart = tree.DecisionTreeClassifier()

    cart = cart.fit(x,y)

    return cart.predict(input)

if __name__ == '__main__':
    app.run(debug=True)

I am very new to python programming. Please help me with any suggestions or solutions.

Have a nice day

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35

1 Answers1

2

I assume that you want to pass input as a parameter in flask. You cannot define the input as parameters to your flask's endpoint function. Instead, you should read the parameters inside the said function with request.args.get like this:

@app.route('/', methods=['GET','POST'])
def index():
    input = request.args.get('input')
    if input is None:
        input = [[1,1,2,3,3,1,1,2]]

From python docs:

The request object is automatically reachable from all flask api's and it contains all the data that you are passing through the api. To access incoming request data, you can use the global request object. Flask parses incoming request data for you and gives you access to it through that global object. Internally Flask makes sure that you always get the correct data for the active thread if you are in a multithreaded environment.

EDIT:

In the comments we had a similar example with path parameter:

@app.route('/<input>', methods=['GET','POST'])
def index(input):

the final result will be identical to my initial answer above, but it less practical from design standpoint. You would be using a post method to pass an array of data and in this case, the data should come inside a request.

Another reason to not do it is that you should also always avoid passing an array as a path parameter unless

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
  • 1
    You can pass name as a param to flask view only if there is regex in app.route. `@app.route('/', methods=['GET', 'POST']) def index(input):` – Rahul Raut Nov 19 '19 at 06:00
  • @RahulRaut err i didnt understand it, so i must change app = Flask(__name__) to app = Flask (__param__) ? and change the app.route code? – Dewadek Krisna Nov 19 '19 at 06:46
  • @DewadekKrisna I updated my answer to address this question – Simas Joneliunas Nov 19 '19 at 06:53
  • @SimasJoneliunas i already update my code as u suggest but after run on it and go to web but i got 404 not found in web and in cmd i got (typeerror :The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a ndarray) did i doing something wrong? i'm so sry im still new on these thing – Dewadek Krisna Nov 19 '19 at 07:09
  • The error says that `return cart.predict(input)` the result of this predict is a numpy array. Numpy array is not a valid flask return type. You should thus cast this result to list as in `return cart.predict(input).tolist()`. See: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html – Simas Joneliunas Nov 19 '19 at 07:13
  • i already add it but it still same just change "but it was a list" code, – Dewadek Krisna Nov 19 '19 at 07:45
  • My bad, apparently you need to return the response in json format. See: https://stackoverflow.com/questions/58433450/how-to-send-ndarray-response-from-flask-server-to-python-client – Simas Joneliunas Nov 19 '19 at 07:49
  • @SimasJoneliunas aah but can u at least give the right code for me? oh fyi this server is planned for and android apps client – Dewadek Krisna Nov 19 '19 at 07:56
  • The answer was given in the question that I have linked you~ `return jsonify({'predictions' : predictionsIm.tolist()})` – Simas Joneliunas Nov 19 '19 at 08:00
  • @SimasJoneliunas so i must change return cart.predict(input) into return jsonify({'predictions' : predictionsIm.tolist()}) ? – Dewadek Krisna Nov 19 '19 at 08:02
  • should be `jsonify({'predictions' : cart.predict(input).tolist()})` – Simas Joneliunas Nov 19 '19 at 08:09
  • @SimasJoneliunas aah thank you buddy, no error these time in cmd (still 404 not found in web, maybe cause it server ) – Dewadek Krisna Nov 19 '19 at 08:21