Why can't i see results of flask file in my html file?

34 Views Asked by At

I am working on a market basket analysis project. I am not able to display the results of the flask part in the html page.

HTML FILE (results.html):

<!DOCTYPE html>
<html>
  <head>
    <title>Market basket analysis</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
  </head>
  <body>
    <h1>Market Basket Analysis</h1>
    <table>
      <thead>
        <tr>
          <th>Left Hand Side</th>
          <th>Right Hand Side</th>
          <th>Support</th>
          <th>Confidences</th>
          <th>Lifts</th>
        </tr>
      </thead>
      <tbody>
        {% for result in results %}
        <tr>
          <td>{{ result[0] }}</td>
          <td>{{ result[1] }}</td>
          <td>{{ result[2] }}</td>
          <td>{{ result[3] }}</td>
          <td>{{ result[4] }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </body>
</html>

PYTHON FILE (app.py):

from flask import Flask, render_template
import pandas as pd
from apyori import apriori

app = Flask(__name__)

@app.route('/')
def index():
    df = pd.read_csv('Groceries_data.csv')
    df['date'] = pd.to_datetime(df['Date'])
    cust_level = df[["Member_number","itemDescription"]].sort_values(by="Member_number", ascending= False)
    cust_level["itemDescription"] = cust_level["itemDescription"].str.strip()
    transactions = [a[1]['itemDescription'].tolist() for a in list(cust_level.groupby(["Member_number"]))]
    rules = apriori(transactions = transactions, min_support = 0.002, min_confidence=0.05, min_lift=3, min_length=2)
    results = list(rules)
    
    def inspect(results):
        lhs = [tuple(result[2][0][0])[0] for result in results]
        rhs = [tuple(result[2][0][1])[0] for result in results]
        supports = [result[1] for result in results]
        confidences = [result[2][0][2] for result in results]
        lifts = [result[2][0][3] for result in results]
        return list(zip(lhs,rhs,supports,confidences,lifts))

    resultsindataframe = pd.DataFrame(inspect(results),columns=['Left Hand Side','Right Hand Side','Support','Confidences','Lifts']) 
    results = resultsindataframe.to_html(index=False)
    return render_template('results.html', results=results)

if __name__ == '__main__':
    app.run(debug=True)

FILE POSITIONS :

MBA/
├── app.py
├── templates/
│ └── result.html
└── Groceries_data.csv

OUTPUT: No Results Shown

1

There are 1 best solutions below

0
Elie Saad On

results should be a list and it seems that it is not since you're converting it to html:

results = resultsindataframe.to_html(index=False)

In addition that results may be None.

make sure that when sending results to template it is a list and it contains values. I suggest remove this line of code and retry it. you can print the raw list to make sure of that.

I you want the html as it is then do not remove the above line of code, then remove the loop and do this in the template:

{{results}}

I hope that helped.