Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/flask/Jinja2 and Json

"I am using Flask,Jinja2,higHighcharts"

Example (Python/Flask):

@app.route("/column/")
def column():
    data=[{"data": [49.9, 54.4], "name": "Tokyo"}, {"data": [42, 30.4], "name": "AC"}]
    return render_template('column.html', data=data)

my templates

$(document).ready(function() {
      chart1 = new Highcharts.Chart({
         chart: {
            renderTo: 'container',
            type: 'bar'
         },
         title: {
            text: 'Fruit Consumption'
         },
         xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
         },
         yAxis: {
            title: {
               text: 'Fruit eaten'
            }
         },
         series:{{ data }}
      });
   });

i view highcharts (column.html)

series:[{&\#39;data': [4, 5, 9], &\#39;name&\#39;: &\#39;Jane&\#39;},{&\#39;data&\#39;: [8, 3, 4], &\#39;name&\#39;: &\#39;John&\#39;}]});

i want to correct Jinja2 wording,Ultimately the desired results.

series: [{
            name: 'Jane',
            data: [1, 0, 4]}, {
            name: 'John',
            data: [5, 7, 3]
         }]
like image 390
yiping Avatar asked Jan 05 '13 04:01

yiping


People also ask

Does Jinja2 have Flask?

Flask comes packaged with Jinja2, and hence we just need to install Flask. For this series, I recommend using the development version of Flask, which includes much more stable command line support among many other features and improvements to Flask in general.

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

Is Jinja2 part of Python?

Jinja2 is a library for Python that is designed to be flexible, fast and secure. If you have any exposure to other text-based template languages, such as Smarty or Django, you should feel right at home with Jinja2.


1 Answers

Mark your data as safe with Markup:

Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped.

Or change {{ data }} to {{ data|tojson|safe }}.

like image 89
miku Avatar answered Oct 12 '22 23:10

miku