0

I'm building a web car price predictor with Flask. However, when I click on the submit button of the form, nothing happens and the data is not sent nor processed. My terminal shows the HTTP code 200 and 304 just after running the Flask app.

Here is the HTML form:

<form action= '{{url_for('predict')}}' method="POST" id="form"></form>
    <h3>How much is my car worth?</h3>
    <p>Get an accurate price for your vehicle by entering the following details</p>
        <section class='year'>
            <label for="year">Year</label>
            <input type="number" name="year" id="year1" required='required' min="0">
        </section>
        <section class='msrp'>
            <label for="msrp">MSRP</label>
            <input type="number" name="msrp" id="msrp" required='required' min="0">
        </section>
      <section class='kms'>
            <label for="kms">Kilometers Driven</label>
            <input type="number" name="kms" id="kms" required='required' min="0">
        </section>
        <section class='owner'>
            <label for="owner">Previous Owners</label>
            <select name="owner" id="owner">
                <option value="1">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
              </select>
        </section>
        <section class='fuel-type'>
            <label for="fuel">Fuel Type</label>
            <select name="fuel" id="fuel">
                <option value="Petrol">Petrol</option>
                <option value="Diesel">Diesel</option>
                <option value="CNG">CNG</option>
              </select>
              <section class='seller-type'>
                <label for="seller-type">Dealer or individual?</label>
                <select name="seller-type" id="seller-type">
                    <option value="Dealer">Dealer</option>
                    <option value="Individual">Individual</option>
                  </select>
        </section>
        <section class='transmission-type'>
            <label for="transmission">Transmission</label>
            <select name="transmission" id="transmission">
                <option value="Manual">Manual</option>
                <option value="Automatic">Automatic</option>
                </select>
        </section>
        <section>
            <input type='submit' id='submit'>
        </section>
</form>
<div id="price">
    <p>{{predPrice}}</p>
</div>
</body>
</html>

And here is the Flask app:

import re
from flask import Flask, render_template, request
import pickle
import requests
import numpy as np
import pandas as pd


model = pd.read_pickle(r'predictor/price/vehicles/file.pkl')

app = Flask(__name__)

@app.route("/", methods = ['GET', 'POST'])
def predict():
   if request.method == 'POST': 
       year = int(request.form["year"]) # taking year input from the user
       tot_year = 2020 - year
       present_price = float(request.form["msrp"]) #taking the present prize
       fuel_type = request.form["fuel-type"] # type of fuel of car
       # if loop for assigning numerical values
       if fuel_type == "Petrol":
           fuel = 2
       elif fuel_type == "Diesel":
           fuel= 1
       else:
           fuel = 0
       kms_driven = int(request.form["kms"]) # total driven kilometers of the car
       transmission = request.form["transmission-type"] # transmission type
       # assigning numerical values
       if transmission == "Manual":
           transmission_type = 1
       else:
           transmission_type = 0
       seller_type = request.form["seller_type"] # seller type
       if seller_type == "Individual":
           seller_individual = 1
       else:
           seller_individual = 0
       owner = int(request.form["owner"])  # number of owners
       # X = [['Age', 'Present_Price', 'Kms_Driven', 'Fuel_Type', 'Seller_Type', 'Transmission', 'Owner']]
       values = [[
       tot_year,
       present_price,
       kms_driven,
       fuel,
       seller_individual,
       transmission_type,
       owner,
       ]]
       # created a list of all the user inputed values, then using it for prediction
       prediction = model.predict(values)
       prediction = round(prediction[0],2)
       # returning the predicted value inorder to display in the front end web application
       return render_template("home.html", predPrice = 'xd')
   else:
       return render_template('home.html')

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

The HTML opens in the browser and I can fill the form. I can even click on the Submit button, but nothings happens. Also, there are no messages that appear on the terminal when I click on Submit.

1
  • Inspect your html and check the action property on the form element Commented Sep 29, 2021 at 21:16

3 Answers 3

1

The problem is with your form itself. You didn't place your input elements inside your form element. All your sections must be placed inside your form tag. The other way is to include the form id attribute in all of your inputs. You have given the form an id of "form", so just update your sections with form="form". The following is an example of an updated section.

<section class='year'>
            <label for="year">Year</label>
            <input type="number" name="year" id="year1" form="form" required='required' min="0"
</section>

Keep in mind that the submit button has to be updated as well.

Sign up to request clarification or add additional context in comments.

Comments

1

maybe add name to input do the work?

<input type='submit' id='submit' name="do it">

Comments

0
Mention the post method explicitly for the browser.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.