Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render Arabic strings in Bottle framework?

I am learning Bottle framework and new to Python. Just stumbled upon this difficulty. When I write a simple method to return a an Arabic string like:

@route('/hello')
def hello():
    return u'سلام'

I get this error message in the terminal:

SyntaxError: Non-ASCII character '\xd8' in file hello.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

I have imported all from bottle and tried adding other methods mentioned in the docs where it talks about "Changing the Default Encoding" however I was unable to resolve the issue. So I appreciate your hints.

like image 346
qliq Avatar asked Sep 09 '11 20:09

qliq


2 Answers

Here is my code for testing:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bottle import *

@route('/hello')
def hello():
    return u'سلام'

run(host='127.0.0.1', port=8080,reloader=True)

In my editor, I choose File > Save As..., then select Unicode (UTF-8) as Text Encoding, and saved as hello.py

Then download the lastest version of bottle.py from github, and put it in the same folder(e.g. bottle-test) with hello.py

Run it, and seems no problems at all.

~$ python --version
Python 2.6.7
~$ cd bottle-test
bottle-test$ python hello.py 

Result in browser

like image 110
Nianliang Avatar answered Oct 10 '22 10:10

Nianliang


just add

# -*- coding: whatever-encoding-you-use -*-

on the top of your file

like image 5
JBernardo Avatar answered Oct 10 '22 08:10

JBernardo