Added unicode chapter to the docs. This fixes #67
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ea72e27 · Browse files · 2 files·+82 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+82 added−0 removed
- other:+82 added−0 removed
Provenance
- Commit
387be22- Tree
d77847f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ea72e27
2 files changed+82 added−0 removed
modified:docs/contents.rst.inc+1 added
40 unmodified lines41 42 design43 htmlfaq44 security45+ unicode46 extensiondev47 license48 upgrading49 changelogadded:docs/unicode.rst+81 added
1+Unicode in Flask2+================3+4+Flask like Jinja2 and Werkzeug is totally unicode based when it comes to5+text. Not only these libraries, also the majority of web related Python6+libraries that deal with text. If you don't know unicode so far, you7+should probably read `The Absolute Minimum Every Software Developer8+Absolutely, Positively Must Know About Unicode and Character Sets9+<http://www.joelonsoftware.com/articles/Unicode.html>`_. This part of the10+documentation just tries to cover the very basics so that you have a11+pleasent experience with unicode related things.12+13+Automatic Conversion14+--------------------15+16+Flask has a few assumptions about your application (which you can change17+of course) that give you basic and painless unicode support:18+19+- the encoding for text on your website is UTF-820+- internally you will always use unicode exclusively for text except21+ 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.24+25+So what does this mean to you?26+27+HTTP is based on bytes. Not only the protocol, also the system used to28+address documents on servers (so called URIs or URLs). However HTML which29+is usually transmitted on top of HTTP supports a large variety of30+character sets and which ones are used, are transmitted in an HTTP header.31+To not make this too complex Flask just assumes that if you are sending32+unicode out you want it to be UTF-8 encoded. Flask will do the encoding33+and setting of the appropriate headers for you.34+35+The same is true if you are talking to databases with the help of36+SQLAlchemy or a similar ORM system. Some databases have a protocol that37+already transmits unicode and if they do not, SQLAlchemy or your other ORM38+should take care of that.39+40+The Golden Rule41+---------------42+43+So the rule of thumb: if you are not dealing with binary data, work with44+unicode. What does working with unicode in Python 2.x mean?45+46+- 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 mark50+ this string as unicode string by prefixing it with a lowercase `u`.51+ (like ``u'Hänsel und Gretel'``)52+- if you are using non-unicode characters in your Python files you have53+ to tell Python which encoding your file uses. Again, I recommend54+ UTF-8 for this purpose. To tell the interpreter yuor encoding you can55+ put the ``# -*- coding: utf-8 -*-`` into the first or second line of56+ your Python source file.57+58+Encoding and Decoding Yourself59+------------------------------60+61+If you are talking with a filesystem or something that is not really based62+on unicode you will have to ensure that you decode properly when working63+with unicode interface. So for example if you want to load a file on the64+filesystem and embedd it into a Jinja2 template you will have to decode it65+form the encoding of that file. Here the old problem that textfiles do66+not specify their encoding comes into play. So do yourself a favour and67+limit yourself to UTF-8 for textfiles as well.68+69+Anyways. To load such a file with unicode you can use the builtin70+:meth:`str.decode` method::71+72+ def read_file(filename, charset='utf-8'):73+ with open(filename, 'r') as f:74+ return f.read().decode(charset)75+76+To go from unicode into a specific charset such as UTF-8 you can use the77+:meth:`unicode.encode` method::78+79+ def write_file(filename, contents, charset='utf-8'):80+ with open(filename, 'w') as f:81+ f.write(contents.encode(charset))