This article mainly introduces the introduction and installation methods of the Python Flask framework. Flask is a very popular and concise web development framework, for more information about how to use Flask, see. I also hope that this article will answer questions about the initial intention and objectives of the Flask project and the applicable scenarios (Situations) of the flask project.
What is "micro "?
"Micro" does not mean to put the entire Web application into a Python file, although it does. Of course, "Micro" does not mean that Flask functions are insufficient. In a microframework, "Micro" means that Flask is designed to keep the code concise and easy to expand. Flask won't make too many choices for you, such as what database to choose. Flask is easy to modify for you, such as the template engine you choose. Everything else depends on you, so Flask can meet your needs.
By default, Flask does not include the database abstraction layer, which can be processed by form verification or any other existing library (Django. On the contrary, Flask supports extensions, which can be added to your application, as is implemented by Flask itself. Many extensions provide functions such as database integration, form verification, upload processing, and a variety of open authentication technologies. Flask may be a "micro" type, but it can already be produced and used in a variety of needs.
Configurations and conventions
Flask has many configuration items with reasonable default values and follows some conventions. For example, by convention, templates and static files are stored in subdirectories under the Python source code tree of the application, which can be changed. You generally do not have to do this, especially at the beginning.
Grow together with Flask
Once your Flask project is built and run, you will find that a large number of available extensions in the Community are integrated into your production environment project. The Flask Core Team reviews these extensions to ensure that verified extensions can still be used in future versions.
As your code library grows, you are free to make proper design decisions for your project. Flask will continue to provide a simple bonding layer as much as possible in Python. You can implement advanced mode in SQLAlchemy or other database tools. When appropriate, introduce non-relational data persistence and use WSGI tools unrelated to the Framework. WSGI is a Python web interface.
Install
Flask depends on two external libraries, Werkzeug and Jinja2. Werkzeug is a WSGI tool set, which is a standard interface between web applications and servers for development and deployment. Jinja2 is responsible for rendering templates.
So how can we install it quickly? You have many ways to install it, but the simplest and most crude method is virtualenv. Let's first look at it.
Virtualenv
Maybe Virtualenv is your preferred choice in development. If you have the shell permission on the production machine, you will also be willing to use virtualenv.
What problems does virtualenv solve? If you like Python like me, there are many opportunities to use Python on projects other than Flask-based web applications. However, the more projects there are, the more likely it is to work in different versions of python, or at least on versions of different python libraries. We need to face the fact that the database damages backward compatibility is quite common, and formal applications with zero dependencies are unlikely to exist. So what do you do when two or more dependencies in your project conflict?
The emergence of Virtualenv solves all this problem! Virtualenv allows multiple Python installations of different versions, each of which serves its own project. It does not actually install an independent Python copy, but provides a way to keep the environment independent. Let's see how virtualenv works.
If you are running Mac OS X or Linux, the following two commands may apply:
$ sudo easy_install virtualenv
Or better:
$ sudo pip install virtualenv
The above command will install virtualenv in your system. It may even appear in the package manager. If you are using Ubuntu, try:
$ sudo apt-get install python-virtualenv
If the easy_install command is not installed in Windows, you must install easy_install first. Once easy_install is installed, run the preceding command, but remove the sudo prefix.
Once virtualenv is successfully installed, run shell to create your own environment. I usually create a project folder under which the venv folder is created:
$ mkdir myproject$ cd myproject$ virtualenv venv
New python executable in venv/bin/pythonInstalling distribute............done.
Now, if you want to work on a project, you only need to activate the corresponding environment. In OS X and Linux, do the following:
$ . venv/bin/activate
If you are a Windows user, the following command line is prepared for you:
$ venv\scripts\activate
Either way, you can now use your virtualenv (note that your shell prompt displays the active environment ).
Now you only need to enter the following command to activate Flask in your virtualenv:
$ pip install Flask
In a few seconds, everything will be ready for you.
Global Installation
This is also possible, although I do not recommend it. Run pip as root:
$ sudo pip install Flask
(In Windows, run this command in the command prompt with administrator privileges, and do not need sudo .)
Experience the latest Flask (Living on the Edge)
If you want to work with the latest Flask version, there are two ways: You can use pip to pull the development version or let it operate on a git checkout. However, virtualenv is recommended.
Get a git checkout on a new virtualenv and run it in Development Mode:
$ git clone http://github.com/mitsuhiko/flask.git
Initialized empty Git repository in ~/dev/flask/.git/
$ cd flask$ virtualenv venv --distribute
New python executable in venv/bin/pythonInstalling distribute............done.
$ . venv/bin/activate$ python setup.py develop
...Finished processing dependencies for Flask
This will pull the dependency and activate git head as the current version in virtualenv. Then you only need to execute git pull origin to upgrade to the latest version.
You do not have git to obtain the latest development version. You need to do this:
$ mkdir flask$ cd flask$ virtualenv venv --distribute$ . venv/bin/activate
New python executable in venv/bin/pythonInstalling distribute............done.
$ pip install Flask==dev
...Finished processing dependencies for Flask==dev
Pip and distribute in Windows
Installing easy_install in Windows is tricky, but it is still very simple. The simplest way is to download the distribute_setup.py file and run it. The simplest way to run this file is to open the download folder and double-click the file.
Then, add the Scripts folder of Python to the path environment variable, so that the easy_install command and other Python Scripts are added to the PATH automatically searched by the command line. To do this, right-click the "my computer" icon on the desktop or in the "Start" menu, select "properties", and click "Advanced System settings" (in Windows XP, click the "advanced" tab), click the "environment variables" button, double-click the "Path" variable in the "System variables" column, and add the Scripts folder of Your Python interpreter. Make sure that you use semicolons to separate it from existing values. Suppose you are using Python 2.7 and the default directory, add the following values:
;C:\Python27\Scripts
This is done! To check whether it works properly, open the command prompt and execute easy_install. If User Account Control is enabled in Windows Vista or Windows 7, the system prompts administrator permission.
Now easy_install has been installed. You can use it to install pip:
> easy_install pip