unicode.rst
This file
Path
docs/unicode.rst
107 lines · 4.5 KiB
Last changed by
Minor spelling fixesJason Daviesbefore adoptioned517c7
History of this fileTake it
The walk stopped after 200 commits: lines older than f5b8c08 point at it.
endpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 1–3why, one step back1Unicode in Flask2================3Minor spelling fixesJason Daviesbefore adoptioned517c7
line 4why, one step back4Flask like Jinja2 and Werkzeug is totally Unicode based when it comes toendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
line 5why, one step back5text. Not only these libraries, also the majority of web related PythonMinor spelling fixesJason Daviesbefore adoptioned517c7
line 6why, one step back6libraries that deal with text. If you don't know Unicode so far, youendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 7–10why, one step back7should probably read `The Absolute Minimum Every Software Developer8Absolutely, Positively Must Know About Unicode and Character Sets9<http://www.joelonsoftware.com/articles/Unicode.html>`_. This part of the10documentation just tries to cover the very basics so that you have aMinor spelling fixesJason Daviesbefore adoptioned517c7
line 11why, one step back11pleasant experience with Unicode related things.endpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 12–16why, one step back1213Automatic Conversion14--------------------1516Flask has a few assumptions about your application (which you can changeMinor spelling fixesJason Daviesbefore adoptioned517c7
line 17why, one step back17of course) that give you basic and painless Unicode support:endpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 18–19why, one step back1819- the encoding for text on your website is UTF-8Minor spelling fixesJason Daviesbefore adoptioned517c7
line 20why, one step back20- internally you will always use Unicode exclusively for text exceptendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 21–31why, one step back21 for literal strings with only ASCII character points.22- encoding and decoding happens whenever you are talking over a protocol23 that requires bytes to be transmitted.2425So what does this mean to you?2627HTTP is based on bytes. Not only the protocol, also the system used to28address documents on servers (so called URIs or URLs). However HTML which29is usually transmitted on top of HTTP supports a large variety of30character sets and which ones are used, are transmitted in an HTTP header.31To not make this too complex Flask just assumes that if you are sendingMinor spelling fixesJason Daviesbefore adoptioned517c7
line 32why, one step back32Unicode out you want it to be UTF-8 encoded. Flask will do the encodingendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 33–36why, one step back33and setting of the appropriate headers for you.3435The same is true if you are talking to databases with the help of36SQLAlchemy or a similar ORM system. Some databases have a protocol thatMinor spelling fixesJason Daviesbefore adoptioned517c7
line 37why, one step back37already transmits Unicode and if they do not, SQLAlchemy or your other ORMendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 38–43why, one step back38should take care of that.3940The Golden Rule41---------------4243So the rule of thumb: if you are not dealing with binary data, work withMinor spelling fixesJason Daviesbefore adoptioned517c7
line 44why, one step back44Unicode. What does working with Unicode in Python 2.x mean?endpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 45–49why, one step back4546- as long as you are using ASCII charpoints only (basically numbers,47 some special characters of latin letters without umlauts or anything48 fancy) you can use regular string literals (``'Hello World'``).49- if you need anything else than ASCII in a string you have to markMinor spelling fixesJason Daviesbefore adoptioned517c7
line 50why, one step back50 this string as Unicode string by prefixing it with a lowercase `u`.endpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
line 51why, one step back51 (like ``u'Hänsel und Gretel'``)Minor spelling fixesJason Daviesbefore adoptioned517c7
line 52why, one step back52- if you are using non-Unicode characters in your Python files you haveendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 53–63why, one step back53 to tell Python which encoding your file uses. Again, I recommend54 UTF-8 for this purpose. To tell the interpreter your encoding you can55 put the ``# -*- coding: utf-8 -*-`` into the first or second line of56 your Python source file.57- Jinja is configured to decode the template files from UTF-8. So make58 sure to tell your editor to save the file as UTF-8 there as well.5960Encoding and Decoding Yourself61------------------------------6263If you are talking with a filesystem or something that is not really basedMinor spelling fixesJason Daviesbefore adoptioned517c7
lines 64–65why, one step back64on Unicode you will have to ensure that you decode properly when working65with Unicode interface. So for example if you want to load a file on theendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 66–70why, one step back66filesystem and embed it into a Jinja2 template you will have to decode it67from the encoding of that file. Here the old problem that text files do68not specify their encoding comes into play. So do yourself a favour and69limit yourself to UTF-8 for text files as well.70Minor spelling fixesJason Daviesbefore adoptioned517c7
line 71why, one step back71Anyways. To load such a file with Unicode you can use the built-inendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 72–77why, one step back72:meth:`str.decode` method::7374 def read_file(filename, charset='utf-8'):75 with open(filename, 'r') as f:76 return f.read().decode(charset)77Minor spelling fixesJason Daviesbefore adoptioned517c7
line 78why, one step back78To go from Unicode into a specific charset such as UTF-8 you can use theendpoint is optional for modules. This fixes #86Armin Ronacherbefore adoptionf5b8c08
lines 79–107why, one step back79:meth:`unicode.encode` method::8081 def write_file(filename, contents, charset='utf-8'):82 with open(filename, 'w') as f:83 f.write(contents.encode(charset))8485Configuring Editors86-------------------8788Most editors save as UTF-8 by default nowadays but in case your editor is89not configured to do this you have to change it. Here some common ways to90set your editor to store as UTF-8:9192- Vim: put ``set enc=utf-8`` to your ``.vimrc`` file.9394- Emacs: either use an encoding cookie or put this into your ``.emacs``95 file::9697 (prefer-coding-system 'utf-8)98 (setq default-buffer-file-coding-system 'utf-8)99100- Notepad++:101102 1. Go to *Settings -> Preferences ...*103 2. Select the "New Document/Default Directory" tab104 3. Select "UTF-8 without BOM" as encoding105106 It is also recommended to use the Unix newline format, you can select107 it in the same panel but this is not a requirement.