在烧瓶上部署后如何保持输入数据显示?

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

我正在使用 FLASK 使用逻辑回归模型部署变量。应用程序中的变量计算正确,一切似乎都运行良好,但有一个小问题,当我单击“打印团队费率”按钮时,“主队”和“客队”的名称就消失了。我希望他们在显示预测后留下来。任何的想法?提前致谢。

点击前...

点击按钮后...

这没有意义...我希望保留这两个名字。

<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)

提前致谢

您可以将值传递给您的输入。 .get('key','msg if key is not there')。您可以使用 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" />