Search code examples
pythonpython-3.xflaskunicodeflask-restful

Flask : Unable to render Unicode Characters in Webpage


I am trying to print devnagari (hindi) on a webpage using flask. However, on browsing to the webpage, the unicode I am passing is converted to a string. Program -

from flask import jsonify, Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class Text(Resource):
    def get(self):
        textInput = u'\u0960'
        return textInput

api.add_resource(Text, '/')

if __name__ == '__main__':
    app.run(debug=True)

Output :

"\u0960"

Expected Output :


Solution

  • If you use Flask's make_response to return a response instance, it works fine:

    from flask import jsonify, Flask, make_response
    from flask_restful import Resource, Api
    
    app = Flask(__name__)
    api = Api(app)
    
    class Text(Resource):
        def get(self):
            textInput = u'\u0960'
            return make_response(textInput)
    
    api.add_resource(Text, '/')
    
    if __name__ == '__main__':
        app.run(debug=True)