1

I'm retrieving data from my flask app as json and trying to turn my json values into a list.

In my html I have the following:

<script>
...
 series: [{
name: 'Installation',
data: [{% for x in results %}{{ x.TestInstallation }}{% endfor %}]},
...
</script>

data: above should have values displayed as a list:

data: [10957, 10372, 10256, 10074, 8816, 8605, 9524, 10888, 11188, 10305, 13367, 10775, 9963, 12758, 13361, 10843]

my current input for data: is the following:

data: [1095710372102561007488168605952410888111881030513367107759963127581336110843]},

How can I turn values assigned to data: into a list?

1
  • 1
    It's a bad practice to generate lines of JavaScript based on the logic of another language. You'd probably want to output the JSON as global variable in JavaScript, or create an endpoint to get the JSON from. Then let JavaScript handle the JSON on the frontend. Commented Oct 4, 2020 at 18:01

1 Answer 1

1

you don't have to loop through the array in the rendering engine, you can pass it as JSON with the JSON filter

@app.route('/view')
def view():
    data = [1,2,3,4,5,6,7]
    # render the template with our data
    return render_template('view.html', page_title = '12345', data=data)

and in the template


<script>
...
 series: [{
name: 'Installation',
data: JSON.parse('{{ data | tojson }}'),
}]
...
</script>

More about Filters : Standard Filters

Sign up to request clarification or add additional context in comments.

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.