1
我想用Flask構建一個預測Web應用程序。該應用程序應該接受用戶輸入,通過python訓練過的模型進行處理,然後在輸入表單旁顯示結果作爲圖表。如何根據表單輸入更新現有的Python Flask網頁?
我的代碼如下所示:
HTML表單:
<form class = "prediction-options" method = "post" action = "/prediction/results">
<!--the input fields-->
</form>
瓶app.py
@app.route("/")
def main():
return render_template('index.html')
@app.route("/prediction/results", methods = ['POST'])
def predict():
input_aqi = float(request.form['aqi'])/272
input_pm2_5 = float(request.form['pm2_5'])/224
input_pm10 = float(request.form['pm10'])/283
input_so2 = float(request.form['so2'])/36
input_no2 = float(request.form['no2'])/110
input_co = float(request.form['co'])/1.83
input_o3 = float(request.form['o3'])/124
input_list = [[input_aqi,input_pm2_5,input_pm10,input_so2,input_no2,input_co,input_o3]]
output_acute_bronchitis = model_acute_bronchitis.predict(input_list)
output_asthma = model_asthma.predict(input_list)
output_asthmatic_bronchitis = model_asthmatic_bronchitis.predict(input_list)
output_aurti = model_aurti.predict(input_list)
output_bronchitis = model_bronchitis.predict(input_list)
output_pneumonia = model_pneumonia.predict(input_list)
d = collections.OrderedDict()
d['acute_bronchitis'] = output_acute_bronchitis[0]
d['asthma'] = output_asthma[0]
d['asthmatic_bronchitis'] = output_asthmatic_bronchitis[0]
d['aurti'] = output_aurti[0]
d['bronchitis'] = output_bronchitis[0]
d['pneumonia'] = output_pneumonia[0]
prediction = jsonify(d)
return prediction
現在,我已成功地採取用戶在「/預測/結果」頁面上輸入並呈現預測結果。我怎樣才能讓結果顯示在'/'頁面上?我試圖做到這一點:
@app.route("/", methods = ['POST','GET'])
def main():
if request.method == 'POST':
def predict():
#predict function that returns prediction
return render_template('index.html')
但我總是得到一個socket.error: [Errno 32] Broken pipe
錯誤消息。我該怎麼辦?