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 ?