Kozea / pygal

PYthon svg GrAph plotting Library
https://www.pygal.org
GNU Lesser General Public License v3.0
2.64k stars 411 forks source link

Data is being duplicated when refresh page #515

Closed andresgiuffre closed 3 years ago

andresgiuffre commented 3 years ago

Hi! I've created a bar chart and embedded it into a web page using base64 and Flask. Data is being retrieved from a csv file and formatted in some way ... I had to sort a dictionary and format it to recognize dates. So everything is fantastic as soon as I start the application. Chart is being displayed and all is great. Now ... if I refresh the page, data is being duplicated in the chart but if I restart the app everything is back to normal, but if I hit refresh many times, data is being added to the original values. Do you know if is there a way I can prevent it?

This is the code:

` @app.route('/pygalexample/') def paygalexample(): try: open_file = open('info/solicitudes_procesos.csv', 'r') for x in open_file: datos_temp = x.split(",") fecha = datos_temp[0] listado_fecha.append(fecha[:5])

    contador = Counter(listado_fecha)

    ordered_data = sorted(contador.items(), key=lambda x: datetime.strptime(x[0], '%d-%m'))

    line_chart = pygal.Bar(height=300, width=800, legend_at_bottom=True, x_label_rotation=45, style=CleanStyle)
    line_chart.title = 'Cantidad de Solicitudes por Día'
    line_chart.x_labels = dict(ordered_data).keys()
    line_chart.add("Solicitudes", dict(ordered_data).values())
    graph_data = line_chart.render_data_uri()

    open_file.close()

    return render_template("graphing.html", graph_data=graph_data)
except Exception as e:
    return (str(e))`

Here it is the normal graph as soon as I start the server

normal

And here it is the same graph after I press refresh on the browser. As you can see, values are being duplicated

after_refreshing

Any ideas?

andresgiuffre commented 3 years ago

What a blind ... I got to reset the original list to an empty one to prevent appending values.

Nevermind, now it's working fine :)