Grizz1618
Grizz1618

Reputation: 23

Flask Application Showing 404 on Post

This is my first attempt at deploying a machine learning application and also my first time using flask. Essentially the user will fill out a form on an html page and the input from the form will be used as input to the machine learning model which is saved in a pickle file, model.pkl in the code below. I am running into one snag that I can't seem to break past..

Every time I submit from index.html and post to result.html I'm receiving a 404 error.

script.py:

#importing libraries
import os
import numpy as np
import flask
import pickle
from flask import Flask, render_template, request

app=Flask(__name__)

@app.route('/')
@app.route('/index')
def index():
    return flask.render_template('index.html')


def ValuePredictor(to_predict_list):
    to_predict = np.array(to_predict_list).reshape(1,12)
    loaded_model = pickle.load(open("model.pkl","rb"))
    result = loaded_model.predict(to_predict)
    return result[0]

@app.route('/result',methods = ['POST'])
def result():
    if request.method == 'POST':
        to_predict_list = request.form.to_dict()
        to_predict_list=list(to_predict_list.values())
        to_predict_list = list(map(int, to_predict_list))
        result = ValuePredictor(to_predict_list)
        if int(result)==1:
            prediction='Income more than 50K'
        else:
            prediction='Income less that 50K'
        return render_template("result.html",prediction=prediction)

index.html:

<html>
<body>
    <h3>Income Prediction Form</h3>

<div>
  <form action="/result.html" method="POST">
    <label for="age">Age</label>
    <input type="text" id="age" name="age">
    <br>
    <label for="edu">Education</label>
    <select id="edu" name="edu">
      <option value="0">High School</option>
      <option value="1">College Degree</option>
    </select>
    <br>
    <label for="martial_stat">Marital Status</label>
    <select id="martial_stat" name="martial_stat">
      <option value="0">not married</option>
      <option value="1">married</option>
    </select>
    <br>
    <label for="gender">Gender</label>
    <select id="gender" name="gender">
      <option value="0">Female</option>
      <option value="1">Male</option>
    </select>
    <br>
    <input type="submit" value="Submit">
  </form>
</div>
</body>
</html>

result.html:

<html>
   <body>
       <h1> {{ prediction }}</h1>
   </body>
</html>

I can't seem to figure this out. My code never seems to reach the first line in the result() function. As soon as I submit from index.html http://127.0.0.1:5000/result.html throws a 404 error. Any suggestions?

Upvotes: 0

Views: 414

Answers (3)

coldy
coldy

Reputation: 2195

Instead of <form action="/result.html" method="POST">
Can Use <form action="{{ url_for('result') }}" method="POST">

Upvotes: 0

Amr Ojjeh
Amr Ojjeh

Reputation: 58

The error is very simple here, the action property in index.html should just be

  <form action="/result" method="POST">

instead of

  <form action="/result.html" method="POST">

You want to use /result to go through your flask function. Hope this helps!

Upvotes: 1

Pascal Bugnion
Pascal Bugnion

Reputation: 4928

You are posting to the endpoint /result.html:

<form action="/result.html" method="POST">

... but your route in flask is defined as /result (with no .html):

@app.route('/result',methods = ['POST'])
def result():
   ...

These two need to match, so consider changing the route to result.html.

Upvotes: 0

Related Questions