【问题标题】:How can I deploy a variable from flask to a cell from a table in html?如何将变量从烧瓶部署到 html 表格中的单元格?
【发布时间】:2020-10-28 03:32:24
【问题描述】:

我正在尝试将变量“预测”从 FLASK 部署到 ID = 预测的单元格。该变量在应用程序中正确计算,我无法在 html 表中部署和显示它......如果您能告诉我如何继续,我将不胜感激。我对模型做了最困难的部分,现在我无法部署它。提前致谢


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)
    
    
    prediction = model.predict(X)

   
    return render_template('index.html')
    
if __name__ == "__main__":
    app.run(host='0.0.0.0',port=8080)

``

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

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

</form>


<td>   
<input id="prediction"  type="number" style="text-align:center"/>
</td>

<td>
<input id="draw_game_odd" type="number" style="text-align:center"/>
</td>

<td>
<input id="away_team_odd" type="number" style="text-align:center"/>
</td>

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    您可以将prediction 变量作为kwarg 传递给render_tempalte

    prediction = model.predict(X)
    
    return render_template('index.html', prediction=prediction)
    

    然后在 HTML 文件中访问/插入预测。 例如

    <div>
        {{ prediction }}
    </div
    

    查看关于如何在 HTML https://jinja.palletsprojects.com/en/2.11.x/987654321@ 中插入/显示数据的 jinja 文档

    这个 SO 可能会对您有所帮助 https://stackoverflow.com/a/22181298/3358570

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2020-10-09
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多