PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 14, 2022

[FIXED] How to keep the input data on display after deploying on flask?

 July 14, 2022     flask, html, python, web-deployment     No comments   

Issue

I am deploying variables using a logistic regression model using FLASK. The variables are correctly calculated in the app, and everything seems to work fine, with the little problem, that when I click the "Print team rates" button, the names of the "home team" and "away team" just disappear. I would like them to stay after displaying the prediction. Any idea? Thanks in advance.

Before clicking...

enter image description here

After clicking the button...

enter image description here

which makes no sense... I would like both names to stay.

<tr> 
    
    <form action="{{ url_for('predict')}}"method="post">
    
    <td>  
    <div class="autocomplete" style="width:300px;">
    <input id="home_team" type="text" name="team1" required="required" />
    </div>   
    </td>


    <td>   
    <div class="autocomplete" style="width:300px;">
    <input id="away_team" type="text" name="team2" required="required" />
    </div>
    </td>

    <button type="submit" class="btn btn-primary btn-block btn-large">Print team rates</button>

    </form>
    
    
    <td>   
    {{ winner }} 
    </td>
  
    <td>   
    {{ value1 }}
    </td>
    
    <td>
    {{ value2 }}
    </td>
    
    <td>
    {{ value3 }}
    </td>
   

</tr> 


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

app = Flask(__name__)
model = pickle.load(open('logreg.pkl', 'rb'))

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

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

    int_features = [str(x) for x in request.form.values()]
    final_features = np.array(int_features)    
    
    df = pd.read_csv('list_last_update.csv') 
    home_team = df[df['Name']==final_features[0]]
    away_team = df[df['Name']==final_features[1]]
    
    X1 = np.array(home_team[['OVA', 'ATT']])
    
    X2 = np.array(away_team[['OVA', 'ATT']])
    
    X = np.concatenate((X1, X2), axis=None).astype(int)

    
    X = X.reshape(1, -1)
    
    print('X1 = ', X1)
    print('X2 = ', X2)
    print('X = ', X)
    
    prediction = model.predict(X)
    prediction2 = model.predict_proba(X) 
    prob_home_win = round(prediction2[0,2],2) 
    prob_draw_game = round(prediction2[0,1],2)
    prob_away_win = round(prediction2[0,0],2)
    
    return render_template('index.html', winner = prediction, value1 = prob_home_win, value2 = prob_draw_game, value3 = prob_away_win)   # here we tell him what variables we want to send to the html
    
if __name__ == "__main__":
    app.run(host='0.0.0.0',port=8080)

Thanks in advance


Solution

You can pass a value to your inputs. .get('key','msg if key is not there') . You can use request.form.get('team1', '')

<input id="home_team" type="text" value="{{ request.form.get('team1', '') }}" name="team1" required="required" />
    
<input id="away_team" type="text" value="{{ request.form.get('team2', '') }}" name="team2" required="required" />


Answered By - NavaneethaKrishnan
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing