【发布时间】:2017-05-25 05:04:08
【问题描述】:
我有一个 python 烧瓶应用程序,我需要创建一个 java 脚本条形图。我尝试了各种方法,并且我知道我的变量正在传递给 html 文件,因为我已将它们移动到 html 文件中的其他位置及其工作。我需要将这些变量传递到的地方是 html 文件中的脚本标记下。我对所有这些都是新手,是否需要进行一些特殊处理才能显示我的变量,以便可以构建图表?
不确定是否重要,但我传递的变量只是日期和数字。
# this is my python route that calls a function that fetches a date and a
# number from mongodb. This data im trying to display in my graph.
@app.route('/meritgraph/')
def meritgraph():
score_graph_data, score_date = get_chart_data()
return render_template("graphing.html", chart_data=zip(score_date,
score_graph_data))
# This is working for me but I want this to run inside a javascript tag.
<ol>{% for score_date, score_graph_data in chart_data %}
<li>{{score_date|safe}} and {{score_graph_data}}</li>
{% endfor %}
</ol>{%endblock%}
# for the example above I get the following return output.
Wed May 24 18:11:01 PDT 2017 and 100
Tue May 23 14:39:27 PDT 2017 and 77
Tue May 23 14:14:02 PDT 2017 and 62
# This is what I would like to do inside the script tag. Some reason when I
# run it inside the script tag it wont work. Is there anything special I
# need to do here?
<script type="text/javascript">
<ol>{% for score_date, score_graph_data in chart_data %}
<li>{{score_date|safe}} and {{score_graph_data}}</li>
{% endfor %}
</ol>{%endblock%}
</script>
【问题讨论】:
-
尝试用引号括起来 '{{score_date}}' '{{score_graph_data}}'
-
不,那没有用。我试过了,还是一样。我看到图表背景但是图表数据没有通过。
-
有引号吗?
-
我给了这样的
-
"dataProvider": [ { "category": '{{score_date}}', "column-1": '{{score_graph_data}}' }, ]
标签: javascript python html python-3.x flask