我正在尝试创建一个 Pygal 图表并将其显示在 flask 中 - 不保存 .svg 文件。这可能吗?我尝试过的每个组合都给了我一个错误。 模板:
{% extends "base.html" %}
{% block content %} {{chart}} {% endblock %}
观点:
@app.route('/chart')
def test():
bar_chart = pygal.HorizontalStackedBar()
bar_chart.title = "Remarquable sequences"
bar_chart.x_labels = map(str, range(11))
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])
chart = bar_chart.render()
return render_template('test.html', chart=chart )
谁能告诉我我做错了什么?
最佳答案
如果你使用的是 Python 2.7,你将需要使用这个:
@app.route('/chart')
def test():
bar_chart = pygal.HorizontalStackedBar()
bar_chart.title = "Remarquable sequences"
bar_chart.x_labels = map(str, range(11))
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])
chart = bar_chart.render(is_unicode=True)
return render_template('test.html', chart=chart )
并在模板中呈现图形:
{% extends "base.html" %}
{% block content %} {{chart|safe}} {% endblock %}
关于python - 在 Flask 中生成动态 Pygal 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195290/