Flask basis Introduction

A: Flask Profile

Flask is a Web framework written in a micro Python, Python language so that we can use to quickly implement a Web site or Web service, before the introduction Flask at first to talk about it and Django ties and differences, django a large and comprehensive web framework that many built-in module, flask is a small but excellent lightweight framework, Django features large and comprehensive, Flask contains only basic configuration, the idea of ​​one-stop solution Django, allowing developers do not develop it before selecting application It spends a lot of time on infrastructure. There Django templates, forms, routing, basic database management and so on built-in features. In contrast, Flask just a kernel, the default is dependent on two external libraries: Jinja2 template engine and WSGI toolset --Werkzeug, flask of use is an essential feature of all the tools rely on the form of import to expand, flask reservation only web development of core functionality.

WSGI (web server gateway interface) is used to specify how the python web server how to communicate with the standard Python Web program with python Web server, essentially a socket server. And Werkzeug WSGI module is a specific implementation

Keywords : a micro web framework written in Python and a two core libraries (Jinja2 template engine and WSGI toolset)

Two: Flask advantage

Substantially meet the performance requirements flask general web development, and the flexibility and extensibility superior to other web frame, fit for various databases are very high

Keywords : 1 2 performance basically meet the demand flexibility and strong scalability are high on 3. fit a variety of databases.

4. in a real production environment, the development of small projects fast, flexible design of large projects

Three: Virtual Environment

1: Benefits

Virtual environment is isolated from the Python interpreter environment. By creating a virtual environment, you can have a standalone Python interpreter environment, the equivalent of a private copy of a copy of the python interpreter environment globally, the benefits of doing so is to create standalone Python interpreter environment for each project, because different projects often rely on different versions of the library or Python version. Use the virtual environment can be kept clean global environment Python interpreter to avoid confusion packages and versions, and can be easily distinguished and rely on records of each project, the so-called environmental Tracing the source file is, since it is to copy the file to support various platforms so while improving portability to reproduce depend on the environment in the new environment.

for example:

Example 1: If you have a lot of projects at the same time, there is a reptile project, there is a Flask project, there is a Django project on a environment, the management-related third-party libraries inevitable confusion.

Example 2: If you have two Flask projects, but the two projects flask inconsistent version, version conflicts occur

Keywords : 1, a private copy of the Python interpreter 2 to solve the confusion package management, version conflicts, improve the portability

2: use

(1) environment to build

# Installation process windows development environment, we are using virtualenv virtual development environment, first install the relevant packages depend 
PIP install virtualenvwrapper -win

(2) create the environment

mkvirtualenv + virtual environment name

(3) Common Commands

01, switch to the specified virtual environment: Note that we enter the virtual environment is needed workon command, but the success of the first installation will automatically enter the virtual environment.

workon + virtual environment name

02, exit the virtual environment

deactivate

03, delete the specified virtual environment

rmvirtaulenv + virtual environment name

04, lists all the virtual environment:

lsvirtualenv

05, to the directory where the virtual environment:

cdvirtualenv

Four: Flask use

1: mounting frame flask

# Installation flask frame 
pip install flask

2: Create a flask Framework

2.1: Step

2.2: Step

 3: Reading Folder

(1) static: static pictures stored css js folder

(2) Templates: storing html tags html, etc.

(3) app.py: Project Initiation Document app name can be arbitrarily taken

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

4: Start the file code reading

4.1

from flask import Flask

app = Flask(__name__)
'''
导入我们安装好的flask包,通过flask包导入Flask类,Flask类即为Flask的核心,实例化这个Flask类的到一个实例化对象app。
__name__这个特殊的参数:Python会根据所处的模块来赋予__name__变量相应的值,对于我们的程序来说(app.py),这个值为app。
'''

4.2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/SR-Program/p/11837490.html