在Flask中生成动态Pygal图表

tau*_*ich 1 python flask pygal

我正在尝试创建一个Pygal图表并将其显示在烧瓶中 - 而不保存.svg文件.这可能吗?我试过的每一个组合都给了我一个错误.模板:

{% extends "base.html" %}
{% block content %} {{chart}} {% endblock %}
Run Code Online (Sandbox Code Playgroud)

浏览次数:

@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 )
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么?

kyl*_*e.a 8

如果您使用的是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 )
Run Code Online (Sandbox Code Playgroud)

并在模板中呈现图形:

{% extends "base.html" %}
{% block content %} {{chart|safe}} {% endblock %}
Run Code Online (Sandbox Code Playgroud)