烧瓶脚本
from flask import Flask, render_template, request
import os
import sys
import json
data_raw = [('0', '1', '0', '0'), ('0', '0', '1', '0'), ('1', '0', '0', '0')]
app = Flask(__name__)
@app.route('/')
def index():
return render_template('test.html', data=map(json.dumps, data_raw))
html / js script test.html
{% extends "index.html" %}
{% block content %}
<p id="test">info</p>
<script>
var data_flask = {{ data }};
</script>
{% endblock %}
未捕获的SyntaxError:意外的令牌&amp;
目标是像这样存储data_flask
var data_flask = [["0", "1", "0", "0"],["0", "0", "1", "0"],["1", "0", "0", "0"]]
有什么想法吗?
答案 0 :(得分:0)
Flask与Django一样,默认情况下会自动调整值。您需要使用|safe
过滤器来呈现文字值。
var data_flask = {{ data|safe }};
答案 1 :(得分:0)
你应该map(json.dump, data_raw)
,它应该起作用而不是map(json.dumps, data_raw)