Method Not Allowed (405) The method is not allowed for the requested URL

53 Views Asked by At

I got 405 error after trying to submit in my HTML code from my Flask app to try to predict Car Price. I think the error comes from the Flask app file but I did not found such wrong code

app.py

from flask import Flask, render_template, request
import pandas as pd
import numpy as np
import pickle as pk
from os.path import dirname, join

current_dir = dirname(__file__)
file_path = join(current_dir, "./static/Cleaned Car Dataset.csv")
app = Flask(__name__)
model = pk.load(open('LinearRegression.pkl', 'rb'))
car = pd.read_csv(file_path)

@app.route('/', methods=['GET', 'POST'])
def index():
    companies = sorted(car['company'].unique())
    car_models = sorted(car['name'].unique())
    years = sorted(car['year'].unique(), reverse=True)
    fuel_types = car['fuel_type'].unique()

    return render_template('index.html', companies=companies, car_models=car_models, years=years, fuel_types=fuel_types)

@app.route('/predict', methods=['GET', 'POST'])
def predict():
    if request.method == 'POST':
        company = request.form.get('company')
        car_model = request.form.get('model')
        year = request.form.get('year')
        fuel_type = request.form.get('fuel_type')
        company.insert(0, "Select Company")
        kms_driven = request.form.get('kilo_driven')
        print(company, car_model, year, fuel_type, kms_driven)

        prediction = model.predict(pd.DataFrame(columns=['name', 'company', 'year', 'kms_driven', 'fuel_type'], data=np.array([car_model, company, year, year, kms_driven, fuel_type]).reshape(1, 5)))

        print(prediction)

        return str(np.round(prediction[0], 2))
        
if __name__ == '__main__':
    app.run()
0

There are 0 best solutions below