6

I have Python code, in which I'm using jinja to send data to a template in Flask. I can access the code just find in HTML, but when I try displaying the data in Javascript, it doesn't work. For example, here's my Python code:

name = "Steve"
return render_template('simple.html',data=json.dumps(name))

And in my simple.html code, in the html body:

<script>
var name = {{ data }};
alert(name);
</script>

The error in my console says "SyntaxError: Unexpected token '&'"

I know I've seen this problem before, I'm forgetting how to solve it though.

4 Answers 4

15

Never mind, I got it. I needed to use safe to escape the code. Example:

<script>
var name = {{ data|safe }};
alert(name);
</script>
Sign up to request clarification or add additional context in comments.

Comments

11

Flask got a builtin filter tojson. Therefore we can do:

flask:

data = {
    "firstname": "Steve", 
    "lastname": "Jobs"
}
return render_template('simple.html', data=data)

jinja2:

<script type="text/javascript">
var data = {{ data | tojson }}; 
console.log(data);
</script>

6 Comments

Since all valid JSON is valid JS you can actually skip the JSON.parse step completely and just do `var data = {{ data | tojson }};
Doing this not only make the code look safer, but also parse the json string to a JS object. And JS object is more equal to dict in python.
When a JSON object is written out in a script block (as it is when you do var data = {{ data | tojson }}) it will be interpreted as JavaScript by the browser. Since JSON is a subset of JavaScript the "JSON" that was written out into the script block will be interpreted as a normal JavaScript object by the browser (since JSON is only data, this is safe to do, as opposed to writing out any user-provided string into a script block, which is not safe). Passing the "JSON" as a JavaScript string to JSON.parse is just adding an extra unnecessary step. Does that make sense?
Got you. the single comma can be got rid of too.
I have editeb my answer. The old one is var data = JSON.parse('{{ data | tojson }}');. Thank you, @SeanVieira
|
2

Use it like this.

<script type="text/javascript">
    var data = '{{ invoice.inv_num }}';
</script>

Just put quotes around it for data part.

Comments

0

I was having problems when trying to pass python lists from my Flask app to my external Javascript file (Python/Flask App -> HTML -> Javascript).

This is how I made it work...

app.py

from flask import Flask, render_template, request, redirect, json

@app.route("/login", methods=["POST"])
def login():

    max = 400

    bar_labels = ['JAN', 'FEB', 'MAR', 'APR',
                  'MAY', 'JUN', 'JUL', 'AUG',
                  'SEP', 'OCT', 'NOV', 'DEC']

    bar_values = [967.67, 1190.89, 1079.75, 1349.19,
                  2328.91, 2504.28, 2873.83, 4764.87,
                  4349.29, 6458.30, 9907, 16297]

    return render_template('profile.html', 
                            labels=json.dumps(bar_labels), 
                            values=json.dumps(bar_values), 
                            headings=headings, data=data)

profile.html

.
.
.
<script type="text/javascript"> window.max = JSON.parse({{ max | tojson }});
                                window.values = JSON.parse({{ values | tojson }});
                                window.labels = JSON.parse({{ labels | tojson }})</script>
<script src="{{url_for('static', filename='main.js')}}"></script>
.
.
.

main.js

window.onload=function(){
    console.log(max)
    console.log(labels)
    console.log(values)
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.