Search code examples
pythonflaskunicodejinja2repr

Unicode bug flask jinja2


I am looking to create a web page with python back on flask, everything is working greatly and i'd recommand flask greatly. But when it comes to Unicode et encoding its always hard between python the webpage etc..

So i have a form that i post at a specific flask route, i get my my values and i need to do some little wrapper to get my variables in the good order and all.

I got this dict:

            task_formatted.append(str(item['entity']))

I transform it to a str then i append it to a list so i can easely pass it to my template

I'd expect the str to be render as UTF-8 on the webpage python page:

  # -*- coding: utf-8 -*- 

html page:

  <meta charset="utf-8"/>

i then print them in my page using jinja:

            {% for item in task %}
            <tr>
              <td>{{item[0].decode('utf-8')}}</td>
              <td>{{item[1].decode('utf-8')}}</td>
              <td>{{item[2]}}</td>
              <td>{{item[3]}}</td>
              <td>{{item[4]}}</td>
              <td><button id="taskmodal1"></td>
            </tr>
            {% endfor %}

but my item[0].decode('utf-8') and my item[1].decode('utf-8')

are printing :

{'type': 'Asset', 'id': 1404, 'name': 'Test-Asset comm\xc3\xa9'}

instead of

{'type': 'Asset', 'id': 1404, 'name': 'Test-Asset commé'}

I have tried several ways with .encode('utf-8') on the python side with unicode(str) with render_template().encode('utf-8') And i am growing out of ideas.

To be fair i think their is something i didn't understand with Unicode so i'd like to get some explanations (not documentation link because i most likely already read them) or some solutions to get it working,

its very important for my program to be able to write properly the str has i use it after in js http calls.

Thanks

PS: I am using python2


Solution

  • I found a solution for my problem:

    unicodedata.normalize('NFKD', unicode(str(item['entity']['type']) + str(item['entity']['name']),'utf-8'))
    

    first i transforme my dict to a string with str() then i turn it in UTF-8 Unicode with unicode('str' , 'utf-8') end finaly after importing unicodedata i use unicodedata.normalize()

    Hope it'll help poeple