I am rendering/displaying a list of predictions to a webpage using FLASK API. The list has three elements: ReqNum, Predictions and the probability of the predictions. I am able to show each record in a row properly. But, I can't divide the row into three columns for each of the above mentioned elements. I am converting the data frame to list and then passing it to the webpage. Below is the code:
from flask import Flask, abort, request,render_template, json, render_template_string
from DataPreparationv4 import Data_Preprocess
import numpy as np
import pandas as pd
import pickle
pd.options.mode.chained_assignment = None
filename = 'Test2.pkl'
loaded_model = pickle.load(open(filename, 'rb'))
app = Flask(__name__)
@app.route("/", methods=['GET'])
def Predictions():
Base_Data = pd.read_csv('Test.csv')
DataSet1 = Data_Preprocess(Base_Data)
CaseNumber = DataSet1[1]
DataSet1 = DataSet1[0]
result = loaded_model.predict(DataSet1)
prob = loaded_model.predict_proba(DataSet1)
Predictions = pd.DataFrame({'CTA Code':result,'Prob1':prob[:,0],'Prob2':prob[:,1]})
Predictions['Probability'] = np.where(Predictions['Prob1'] > Predictions['Prob2'],
Predictions['Prob1'], Predictions['Prob2'])
Predictions['CaseNumber'] = CaseNumber['Case Number']
CTA_Map = [['Y',1],['N',0]]
CTA_Map = pd.DataFrame(CTA_Map,columns=['CTA Met','CTA Code'],dtype=float)
Predictions = pd.merge(Predictions,CTA_Map[['CTA Code','CTA Met']],on='CTA Code', how='left')
Predictions = Predictions.drop(['CTA Code','Prob1','Prob2'], axis=1)
Predictions = Predictions[['CaseNumber', 'CTA Met', 'Probability']]
df_list = Predictions.values.tolist()
return render_template('hello13.html', my_list=df_list)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug = True)
Below is the code for HTML template:
<html>
<body>
<table>
<tbody>
{# here we iterate over every item in our list#}
{% for item in my_list %}
<tr><td>{{ item }}</td></tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
View on the browser of the output:
- What should I do to get three columns for each element in the table row.
Since my approach of data frame to list leads to loss of column names or column headers. Hence,
- Is there a way to display the data frame so that I do not lose the column names/headers.
- Or how can I pass the column headers to the HTML table column header using my current technique of data frame to list.
Please guide me in how to resolve this issue. I am still a newbie in Python hence my knowledge on concepts are till evolving.
Hence requesting you to please guide me on the concepts also.