第一部分 Introduction to Flask-Flask介绍
第一章 Installation-安装
1.
Flask is a small framework by most standards, small enough to be called a “microframework.” It is small enough that once you become familiar with it, you will likely be able to read and understand all of its source code.
在大多数标准下,Flask是一个小型的框架,小到可以叫做“微形框架”。它足够小,以至于一旦你对它熟悉,你就很可能可以读懂所有Flask的源码。
But being small does not mean that it does less than other frameworks. Flask was designed as an extensible framework from the ground up; it provides a solid core with the basic services, while extensions provide the rest. Because you can pick and choose the extension packages that you want, you end up with a lean stack that has no bloat and does exactly what you need.
但是小并不代表Flask比其他框架逊。Flask设计之初就是一个可扩展的框架;它提供一个有着基本服务的核心,而扩展则提供了剩下的。因为你可以选择你需要的扩展包,最后你会得到一个恰巧满足你需要的,没有膨胀的精益堆栈。
Flask has two main dependencies. The routing, debugging, and Web Server Gateway Interface (WSGI) subsystems come from Werkzeug, while template support is provided by Jinja2. Werkzeug and Jinja2 are authored by the core developer of Flask.
Flask有两个主要的依赖,路由,调试和WSGI的子系统来自Werkzeug,而模板支持来自Jinja2。Werkzeug和Jinja2都是Flask的核心开发者创作的。
There is no native support in Flask for accessing databases, validating web forms, authenticating users, or other high-level tasks. These and many other key services most web applications need are available through extensions that integrate with the core packages. As a developer, you have the power to cherry-pick the extensions that work best for your project or even write your own if you feel inclined to. This is in contrast with a larger framework, where most choices have been made for you and are hard or sometimes impossible to change.
FLask中没有自带的数据库支持,验证web表单,验证用户或者其他高级的任务。大部分web应用需要的这些,还有其他关键的服务可以在那些与核心包整合的扩展中找到。作为一个开发者,你有权力选择最好的,对你的项目来说最合适的扩展,或者如果你倾向,甚至写你自己的。这与更大的框架做对比,因为在它们中,大部分选择都为你选好了,并且很难,有时甚至不可能改变。
In this chapter, you will learn how to install Flask. The only requirement you need is a computer with Python installed.
在这一章中,你会学习如何安装Flask,唯一的要求就是你需要一台安装了Python的电脑。
Note: The code examples in this book have been verified to work with Python 2.7 and Python 3.3, so using one of these two versions is strongly recommended.
注:书中的代码示例都被验证在Python2.7和3.3中运行,所以用这两个中的一个版本是强烈推荐的。
2.Using Virtual Environments-使用虚拟环境
The most convenient way to install Flask is to use a virtual environment. A virtual environment is a private copy of the Python interpreter onto which you can install packages privately, without affecting the global Python interpreter installed in your system.
最方便的安装Flask的方法是使用虚拟环境。虚拟环境的一个私人的Python解释器副本,在其中你 可以私密地安装包而不影响安装在你系统中的全局Python解释器。
Virtual environments are very useful because they prevent package clutter and version conflicts in the system’s Python interpreter. Creating a virtual environment for each application ensures that applications have access to only the packages that they use, while the global interpreter remains neat and clean and serves only as a source from which more virtual environments can be created. As an added benefit, virtual environments don’t require administrator rights.
虚拟环境很有用因为它们可防止系统Python解释器中包的混乱和版本冲突。给每一个应用创建一个虚拟环境确保应用只能访问自己要用的包的权限,而全局解释器保持整洁(neat),干净并只用作一个创建更多虚拟环境的来源。另一个好处就是,虚拟环境不要求管理员权限。
Virtual environments are created with the third-party virtualenv utility. To check whether you have it installed in your system, type the following command:
虚拟环境由第三方virtualenv创建。要检查是否在系统中安装了它,输入此命令:
$ virtualenv --version
If you get an error, you will have to install the utility.
如果报错了,你需要安装它。
Note: Python 3.3 adds native support of virtual environments through the venv module and the pyvenv command. pyvenv can be used instead of virtualenv, but note that virtual environments created with pyvenv on Python 3.3 do not include pip, which needs to be installed manually. This limitation has been removed in Python 3.4, where pyvenv can be used as a complete virtualenv replacement.
注:Python3.3通过虚拟环境模块(venv module)和pyvenv指令添加了对虚拟环境的自带支持。pyvenv可以取代virtualenv的使用,但注意Python3.3中用pyvenv创建的虚拟环境不包含pip,而pip需要被手动安装。这个限制在Python3.4中移除了,而pyvenv能被用作一个完备的虚拟环境替换。
Most Linux distributions provide a package for virtualenv. For example, Ubuntu users can install it with this command:
大多数Linux发行版提供一个virtualenv的包。比如说,Ubuntu用户可以通过这个指令安装:
$ sudo apt-get install python-virtualenv
If you are using Mac OS X, then you can install virtualenv using easy_install:
如果你在用Mac OS X,那么你可以用easy_install安装virtualenv:
$ sudo easy_install virtualenv
If you are using Microsoft Windows or any operating system that does not provide an official virtualenv package, then you have a slightly more complicated install procedure.
如果你在用Microsoft Windows或者其他不提供官方virtualenv包的OS,那么你将更复杂一点地安装。
Using your web browser, navigate to bitbucket.org/pypa/setupt…, the home of the setuptools installer. In that page, look for a link to download the installer script. This is a script called ez_setup.py. Save this file to a temporary folder on your computer, then run the following commands in that folder:
用你的web浏览器,去github.com/pypa/setupt… (原文给的是bitbucket,但是没了),setuptools安装器的主页。在这一页中,找一个下载安装器脚本的链接。这是个叫做ez_setup.py的脚本。保存这个文件到一个临时文件夹,然后在这个文件夹中运行这个命令
$ python ez_setup.py
$ easy_install virtualenv
Note: The previous commands must be issued from an account with ministrator rights. On Microsoft Windows, start the command prompt window using the “Run as Administrator” option. On Unixbased systems, the two installation commands must be preceded with sudo or executed as the root user. Once installed, the virtualenv util‐ ity can be invoked from regular accounts.
注:前面的命令必须以有管理员权限的用户执行。
Now you need to create the folder that will host the example code, which is available from a GitHub repository. As discussed in “How to Work with the Example Code ” on page xiii, the most convenient way to do this is by checking out the code directly from GitHub using a Git client. The following commands download the example code from GitHub and initialize the application folder to version “1a,” the initial version of the application:
现在你需要创建一个文件夹用来存示范代码,来自一个Github repo。在“How to Work with the Example Code”中(没翻译必要),最方便的方式就是直接用Git客户端连上Github。下列命令从GitHub下载示例代码并且初始化应用文件夹到“1a”版本,应用最初的版本:
$ git clone https://github.com/miguelgrinberg/flasky.git
$ cd flasky
$ git checkout 1a
The next step is to create the Python virtual environment inside the flasky folder using the virtualenv command. This command has a single required argument: the name of the virtual environment. A folder with the chosen name will be created in the current directory and all files associated with the virtual environment will be inside. A commonly used naming convention for virtual environments is to call them venv:
下一步就是用virtualenv命令在flasky文件夹内(作者的这个项目叫做flasky,整部书都是)创建Python虚拟环境。这个命令有一个必须的argument(不知道咋翻译):虚拟环境的名字。一个叫做这个argument的文件夹会在当前目录创建并且所有与虚拟环境有关联的文件夹会在里面。一个常用的虚拟环境名字是venv:
$ virtualenv venv
New python executable in venv/bin/python2.7
Also creating executable in venv/bin/python Installing setuptools............done.
Installing pip...............done.
Now you have a venv folder inside the flasky folder with a brand-new virtual environment that contains a private Python interpreter. To start using the virtual environment, you have to “activate” it. If you are using a bash command line (Linux and Mac OS X users), you can activate the virtual environment with this command:
现在你在flasky文件夹中有一个venv文件夹,里面含着一个私人Python解释器。为了开始用虚拟环境,你需要“激活”它。如果你在用bash命令行(Linux和Mac OS X用户),你可以通过这个命令激活虚拟环境:
$ source venv/bin/activate
If you are using Microsoft Windows, the activation command is:
如果你用Windows,激活命令是:
$ venv\Scripts\activate
When a virtual environment is activated, the location of its Python interpreter is added to the PATH, but this change is not permanent; it affects only your current command session. To remind you that you have activated a virtual environment, the activation command modifies the command prompt to include the name of the environment:
当一个虚拟环境激活了,Python解释器的位置就被添加到PATH中,但这个改变不是永久的;它只影响当前session.为了提醒你已经激活了虚拟环境,激活的命令会改变命令行以包含环境变量名:
(venv) $
When you are done working with the virtual environment and want to return to the global Python interpreter, type deactivate at the command prompt.
当你完成虚拟环境的工作并想要回到全局的Python解释器,在命令行输入deactivate
.
3.Installing Python Packages with pip-用pip安装Python包
Most Python packages are installed with the pip utility, which virtualenv automatically adds to all virtual environments upon creation. When a virtual environment is activated, the location of the pip utility is added to the PATH.
大部分Python包都是用pip程序安装,而virtualenv自动在创建时自动加到所有虚拟环境中.当一个虚拟环境被激活了,pip程序的位置被自动加到PATH里面.
Note: If you created the virtual environment with pyvenv under Python 3.3, then pip must be installed manually. Installation instructions are available on the pip website. Under Python 3.4, pyvenv installs pip automatically
注:如果你用pyenv在Python3.3中创建了虚拟环境,那么pip一定要手动安装.安装教程在pip网站上有.在Python3.4中,pyenv自动安装pip.
To install Flask into the virtual environment, use the following command:
用下面的命令在虚拟环境中安装Flask:
(venv) $ pip install flask
With this command, Flask and its dependencies are installed in the virtual environment. You can verify that Flask was installed correctly by starting the Python interpreter and trying to import it:
用这个命令,Flask和它的依赖会在虚拟环境中安装.你可以通过开启Python解释器并尝试import来验证Flask是否正确安装:
(venv) $ python
>>> import flask
>>>
If no errors appear, you can congratulate yourself: you are ready for the next chapter, where you will write your first web application.
如果没报错,那么可以恭喜你自己了:你已经准备好进入下一章,在那里,你会写下你自己的第一个web应用.
我是原创,不是搬运啊喂!