【发布时间】:2018-12-18 15:19:46
【问题描述】:
我正在尝试使用来自 python 脚本的数据为 Web 应用程序创建交互式图表。
错误是python代码中的数据没有在chart.js脚本上传输或更新,也没有在html上输出。
三个脚本的流程:来自python的标签和数据,进入chart.js以呈现条形图并在html上输出
当我将非洲(数据点)从 2447 更改为 5578 时,图表没有更新。我不确定问题是从 python 转移还是渲染图表。
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>
【问题讨论】:
标签: javascript python flask chart.js