python - 在 Flask 中生成动态 Pygal 图表

标签 python flask pygal

我正在尝试创建一个 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/

相关文章:

python - 由另一个类实例创建的类实例可以访问创建者的成员吗?

python - 在 pydantic 模型中包含非验证方法是不好的做法吗?

Python - 查找列表中第一个空项的索引

javascript - Flask-SocketIO 没有收到消息

python - 如何在 Pygal 中调整工具提示?

python - 将Python 2.7更新到Python 3后,Sublime将无法识别之前导入的模块

python - 如何通过代码使用 Python Alembic 运行迁移?

python - Python 3.4 上的 Flask Bug?如果应用程序包含相对导入,则开发服务器无法运行

python - Flask 渲染 svg 文件,没有交互性

python - Flask-Babel 不使用可插入 View 进行翻译