Python 3.7 TypeError: missing 1 required positional argument: 'input'

Python 3.7 TypeError: missing 1 required positional argument: 'input'

我已经完成了我的功能,但是当我 运行 网络服务器时,我收到以下错误:

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

这是我的代码:

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)

我对 python 编程还很陌生。请帮助我提供任何建议或解决方案。

祝你有愉快的一天

我假设您想将输入作为参数传递到 flask 中。您不能将输入定义为烧瓶端点函数的参数。相反,您应该使用 request.args.get 读取上述函数内的参数,如下所示:

@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]]

来自 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.

编辑:

在评论中我们有一个带有路径参数的类似示例:

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

最终结果将与我上面的初始答案相同,但从设计的角度来看不太实用。您将使用 post 方法来传递数据数组,在这种情况下,数据应该在请求中。

不这样做的另一个原因是您还应该始终避免将数组作为路径参数传递,除非