0

I'm building a simple data analysis web-app using an API endpoint from an ERP.

I want to display the result of my query in a table, so I iter through the results and my data gets displayed nicely. However when I refresh my page, my data gets duplicated.

My problem is the same as in this post.

I have tried to fix my code using the solution of the post above but without success.

This is my route for my Flask app

@app.route('/results', methods=["POST", "GET"])
def results():
    code = hasCode()
    if request.method == "POST":
        quote_reference = request.form["quote"]
        products = resultsList(code, quote_reference)

        try:
            return render_template("results.html", products=products)

        except BaseException:
            return "There was a problem with your request"

    else:
        return render_template("/")

my products are being initiated in the resultList() function and refers to the function underneath:

def resultsList(code, quote_reference):
    """Creates a list of results that can be passed to the template of the
    results.html template
    """
    # initiating the variable
    my_list = {
        'quantity': [],
        'description': [],
        'price': [],
        'img': []
    }

    # The pandalize function fetches the data from the ERP
    query = pandalize(code, quote_reference)

    # Grabbing all reasults from the "pandalize function" and appending them to the list
    for i, row in query.iterrows():
        my_list["quantity"].append(row["quantity"])
        my_list["description"].append(row["name"])
        my_list["price"].append(row["price"])
        my_list["img"].append(row["id"])

    # Zipping the list
    products = zip(
        my_list["quantity"],
        my_list["description"],
        my_list["price"],
        my_list["img"]
    )

    return products

finally, the loop is in the results.html file:

{% for i in products %}
      <tr>
        <td class="quantity">{{ i[0] }}</td>
        <td class="description">{{ i[1] }}</td>
        <td class="price">{{ i[2] }}</td>
        <td class="img">{{ i[3] }}</td>
      </tr>
{% endfor %}

According to this answer I would need to initialize my list (products) inside my resultsList() function for it to work. However I still experience doubling of data.

I don't really understand what I'm doing wrong. Any ideas ?

Nootaku
  • 217
  • 3
  • 14

1 Answers1

0

After checking the results of all functions and going back upstream, it seemed that the original answer was correct. I need to initialize the list each time.

I noticed that my initial list was being populated by my request (the 'pandalize()' function). This had for effect that each time the pandalize function was called, the list became longer and longer.

It has now been fixed.

Nootaku
  • 217
  • 3
  • 14