I am trying to create an interactive chart for a web app with data from python script.
The error is data from the python code is not transferred or updated on the chart.js script and not output on the html.
The flow of the 3 scripts: labels and data from python, goes into the chart.js to render the bar-chart and is output on the html
When I change Africa (data point) from 2447 to 5578, the chart is not updated. I am not sure if the issue is transferring from python or rendering the chart.
Python
from flask import Flask,render_template
app = Flask(__name__)
@app.route("/")
def result():
labels = ["Africa", "Asia", "Europe", "Latin America", "North America"]
data = [5578,5267,734,784,433]
return render_template("result.html", labels=labels, data=data)
if __name__ == '__main__':
app.debug = True
app.run()
script1.js
new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
labels: "{{labels}}",
datasets: [
{
label: "Population (millions)",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: "{{data }}"
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
}
}
});
HTML
<!DOCTYPE html>
<html>
<head>
<title>New</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.min.js">
</script>
</head>
<body>
<h1>Test</h1>
<div class="wrapper">
<canvas id="bar-chart" width="800" height="450"></canvas>
</div>
<script src="{{ url_for('static', filename='script1.js') }}"></script>
</body>
</html>