在Flask模板和前端中获取值时,相同的输入给出不同的输出

问题描述

我的app.py中有一个函数,该函数返回一个值作为输出。当我使用render_template('index.html',prediction_text="Sell at: "{}".format(output))从烧瓶模板中获取输出时,它将返回输出 6.64,这是期望值。但是现在,我不是使用烧瓶模板中的数据来呈现数据,而是使用前端应用程序Angular 9来呈现响应,因此从Angular 9中,我将输入作为表单数据发送到服务器,并且服务器现在需要发送响应返回到json格式的Angular,因此使用json.dumps(output)

但是这样做,我现在得到 9.58 作为输出,而不是 6.64 ,而输入仍然保持不变。从邮递员那里尝试时,将数据作为表单数据发送时,输出也会显示 9.58 。这是项目的github链接https://github.com/krishnaik06/Car-Price-Prediction 我在Flask和python中相对较新,因此将不胜感激。

app.py

standard_to = StandardScaler()
@app.route("/predict",methods=['POST','GET'])
def predict():
    Fuel_Type_Diesel=0
    if request.method == 'POST':
        Year = int(request.form['Year'])
        Present_Price=float(request.form['Present_Price'])
        Kms_Driven=int(request.form['Kms_Driven'])
        Kms_Driven2=np.log(Kms_Driven)
        Owner=int(request.form['Owner'])
        Fuel_Type_Petrol=request.form['Fuel_Type_Petrol']
        if(Fuel_Type_Petrol=='Petrol'):
                Fuel_Type_Petrol=1
                Fuel_Type_Diesel=0
        else:
            Fuel_Type_Petrol=0
            Fuel_Type_Diesel=1
        Year=2020-Year
        Seller_Type_Individual=request.form['Seller_Type_Individual']
        if(Seller_Type_Individual=='Individual'):
            Seller_Type_Individual=1
        else:
            Seller_Type_Individual=0    
        Transmission_Manual=request.form['Transmission_Manual']
        if(Transmission_Manual=='Manual'):
            Transmission_Manual=1
        else:
            Transmission_Manual=0
        prediction=model.predict([[Present_Price,Kms_Driven2,Owner,Year,Fuel_Type_Diesel,Fuel_Type_Petrol,Seller_Type_Individual,Transmission_Manual]])
        output=round(prediction[0],2)
        if output<0:
            return render_template('index.html',prediction_texts="Sorry you cannot sell this car")
        else:
           # return render_template('index.html',prediction_text="You Can Sell The Car at {}".format(output))   # Getting 6.64 as output
            return json.dumps(output)    # Getting 9.58 as output
    else:
        return render_template('index.html')

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

Postman Snippet

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)