I'm a real noobie using the Flask framework (and client-server generally), so bear with me. I have a basic HTML template file with a bit of Flask (uses the {% ... %} notation) passing in a JSON object from a python file. Right now, as a simple sanity check it's outputting the content of motifs
(an array of arrays) as separate lines (<li>
) in an unordered list (<ul>
).
What I want it to do instead is use the motifs
array of arrays in a JavaScript script as data for a visualization I'm trying to do. I tried mingling the Flask script into a JavaScript <script>
tag that iterates out a JavaScript array, but got an error that the script tag didn't like the Flask notation (Uncaught SyntaxError: Unexpected token }
. So how do I get the Flask script to cough up the array for use in a JavaScript script? I realize I may be misunderstanding some things here (perhaps the nature of JSONs?). I'd be grateful for any nudges in the right direction. Thanks!
Below I've included my html template with the Flask script creating the unordered list, I've also included my failed attempt to use the Flask script in a <script>
tag. So how do I successfully access the array (JSON) in a JavaScript?
index.html (a template file - simplified here for clarity):
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<p>Here are some motifs:</p>
<ul>
{% for motif in motifs %}
<li>{{motif}}</li>
{% endfor %}
</ul>
<script>one of a few js scripts</script>
</body>
</html>
The output of the above file looks like this (missing the bullet points which didn't copy):
Here are some motifs:
{'gene1': 1, 'gene2': 1, 'gene3': 7, 'gene4': 7, 'gene5': 1}
{'gene1': 7, 'gene2': 4, 'gene3': 10, 'gene4': 5, 'gene5': 2}
{'gene1': 7, 'gene2': 1, 'gene3': 8, 'gene4': 5, 'gene5': 8}
{'gene1': 2, 'gene2': 4, 'gene3': 6, 'gene4': 1, 'gene5': 9}
{'gene1': 3, 'gene2': 8, 'gene3': 2, 'gene4': 7, 'gene5': 8}
{'gene1': 1, 'gene2': 5, 'gene3': 1, 'gene4': 9, 'gene5': 5}
{'gene1': 3, 'gene2': 5, 'gene3': 6, 'gene4': 10, 'gene5': 9}
{'gene1': 2, 'gene2': 10, 'gene3': 7, 'gene4': 5, 'gene5': 10}
{'gene1': 5, 'gene2': 5, 'gene3': 10, 'gene4': 10, 'gene5': 5}
{'gene1': 10, 'gene2': 4, 'gene3': 4, 'gene4': 6, 'gene5': 4}
Below is my failed attempt to access in a <script>
the array (JSON object) passed by Flask:
<script>
var motifValuesArray = [];
var index = 0; // an iterator to assign indexes in the javascript array
{% for motif in motifs %}
motifValuesArray[index] = {motif};
console.log(motifValuesArray[index]);
{% endfor %}
</script>
, though I would like it to be converted to a JS array rather than printed on screen. Does that make sense?
– gromiczek Sep 16 '13 at 19:27