[英]Generating Dynamic Pygal Chart in Flask
I am trying to create a Pygal chart and display it in flask - without saving the .svg file. 我正在尝试创建一个Pygal图表并将其显示在烧瓶中-不保存.svg文件。 Is this possible? 这可能吗? Every Combination i have tried has given me an error. 我尝试的每个组合都给了我一个错误。 template: 模板:
{% extends "base.html" %}
{% block content %} {{chart}} {% endblock %}
Views: 观看次数:
@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 )
Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?
If you are using Python 2.7 you will need to use this: 如果您使用的是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 )
And to render the graph in the template: 并在模板中呈现图形:
{% extends "base.html" %}
{% block content %} {{chart|safe}} {% endblock %}
import pygal
@app.route('/something.svg')
def graph_something():
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
return bar_chart.render_response()
and then just have your html file refer to that: 然后让您的html文件引用该文件:
<body>
<figure>
<embed type="image/svg+xml" src="/mysvg.svg" />
</figure>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.