I have created a web application using flask and bootstrap. In this application, after putting the inputs and clicking the submit button I want to show the output or result in the same page in a bootstrap modal. But I could not redirect it properly. If there is any solution to it tell me. It will be very helpful. Thanks in advance. The codes are given below.
home.html
<div class="container" align="left">
<div class="panel panel-default" style="border:none;border-radius: 10px;">
<div class="panel-body panel1">
<form method="POST" action="/predict">
<div class="row">
<div class="col-sm-6">
<div class="form-group d2">
<label for="NewYork"><p class="p2">New York</p></label>
<input type="text" class="i1" name="NewYork"><br><br>
<small id="NYHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
<div class="form-group d2">
<label for="california"><p class="p2">California</p></label>
<input type="text" class="i1" name="California"><br><br>
<small id="CaliforniaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
<div class="form-group d2">
<label for="Florida"><p class="p2">Florida</p></label>
<input type="text" class="i1" name="Florida"><br><br>
<small id="FloridaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group d2">
<label for="RnD_Spend"><p class="p2">RnD_Spend</p></label>
<input type="text" class="i1" name="RnD_Spend"><br><br>
<small id="RndSpendHelp" class="form-text text-muted s1">Enter RnD Spendings in US Dollar</small>
</div>
<div class="form-group d2">
<label for="Admin_Spend"><p class="p2">Admin_Spend</p></label>
<input type="text" class="i1" name="Admin_Spend"><br><br>
<small id="AdminSpendHelp" class="form-text text-muted s1">Enter Adminstration Spendings in US Dollar</small>
</div>
<div class="form-group d2">
<label for="Market_Spend"><p class="p2">Market_Spend</p></label>
<input type="text" class="i1" name="Market_Spend"><br><br>
<small id="MarketSpendHelp" class="form-text text-muted s1">Enter Market Spendings in US Dollar</small>
</div>
</div>
</div>
<div class="row" align="right"><input class="btn btn1" type= "submit" value="Submit" data-toggle="modal" data-target="#myModal"></div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
app.py
@app.route("/predict",methods=['GET', 'POST'])
def predict():
NewYork = float(request.form['NewYork'])
California = float(request.form['California'])
Florida = float(request.form['Florida'])
RnD_Spend= float(request.form['RnD_Spend'])
Admin_Spend= float(request.form['Admin_Spend'])
Market_Spend = float(request.form['Market_Spend'])
pred_args =[NewYork, California, Florida, RnD_Spend, Admin_Spend, Market_Spend]
pred_args_arr = np.array(pred_args)
pred_args_arr = pred_args_arr.reshape(1, -1)
mul_reg = open("multiple_linear_model.pkl", "rb")
ml_model = joblib.load(mul_reg)
model_prediction = ml_model.predict(pred_args_arr)
model_prediction = round(float(model_prediction), 2)
return render_template('home.html', prediction = model_prediction)
I would say that it is usually not a good practice to redirect a user after form submission to the same page but with a modal shown (it is just not a good user experience), and I would recommend doing this through the use of AJAX.
Anyway, you can pass a flag that tells the HTML template renderer if it needs to show the modal. Something like this:
Then conditionally render your modal in the template: