The first knowledge of Python lightweight web framework Flask

Source: Internet
Author: User
Tags virtual environment

1. Install the Python package with Pip

Most Python packages are installed using the PIP utility, and the virtual environment created with pyvenv automatically installs the PIP.

1. Use Pip to install flask (other Python packages)

Pip Install flask

  

2. A simple Demo
From flask import flaskfrom flask import abortfrom flask Import Redirectapp = Flask (__name__) @app. Route ('/') def index (): 
   return ' 

  

Learn the concepts of routing, decorators, view functions, requests, and responses from the demo and analyze each line of code in one step:

0.from flask import Flask

Import the Flask function from the Flask Library, from flask import abort and from flask import redirect similarly.

1.app = Flask(__name__)

The constructor for the App,flask class that creates the Flask object has only one parameter that must be specified, that is, the name of the program's main module or package. In most programs, the Python __name__ variable is the desired value.

2.@app.route(‘/‘)

The client (for example, a Web browser) sends the request to the Web server and the Web server sends the request to the Flask program instance. The program instance needs to know what code to run for each URL request, so a mapping of the URL to the Python function is saved. A program that handles the relationship between a URL and a function is called a route .

The easiest way to define a route in a flask program is to use the App.route decorator provided by the program instance to register the decorated function as a route.

decorators are a standard feature of the Python language and can modify the behavior of functions in different ways. The usual usage is to register the function as a handler for the event using a decorator.

3. def index(): functions

The index () function @app.route(‘/‘) is placed behind it, so it is index() registering the function as a route.

The server execution function is triggered when the server domain name of the deployment program is www.example.com accessed in the browser www.example.com index() .

4.@app.route(‘/user/<name>‘)

With @app.route(‘/‘) , if the server domain name of the deployment program is www.example.com , after access in the browser www.example.com/user/tom , the server will be triggered to perform the modifier below sayHello(name) .

5. sayHello(name) functions

If name = = ' Baidu ':        return redirect (' http://www.baidu.com ')    elif name = = ' NO ':        return abort (404)    Return ' 

Assume that the server domain name of the deployment program is www.example.com .

Access in the browser www.example.com/user/baidu , then the redirect() function is directly linked to Baidu.

Note that before you use a redirect() function, you must use the from flask import redirect import function from the Flask library redirect() .

Accessed in the browser www.example.com/user/NO , then the abort() function is triggered.

The abort () function handles the error, abort(404) returns a 404 error, and the abort() function does not return control to the function that called it, but instead throws the exception and gives control to the Web server.

Accessed in the browser www.example.com/user/tom , it will be executed correctly and displayed in the browser Hello,tom .

6.if __name__ == ‘__main__‘:

I did not understand it at first, what is the meaning of this sentence? The answer was found in the StackOverflow.

Roughly doubled:

When the Python interpreter reads the py file, it executes all the code it finds. It defines a number of variables before executing the code. For example, if the py file is the main program, it will set the __name__ variable to "__main__" . If this py is introduced to another module, it __name__ will be set to the name of the module.

Give me a chestnut:
Create a test.py file:

if __name__ == ‘__main__‘:    print ‘自己主动执行‘else:    print ‘我被别的模块引入‘

Execution $ python test.py will output自己主动执行
Enter the subsequent input directly on the command line python import test and output: 我被别的模块引入 .

7.app.run(debug=True)

The program instance launches the Flask inherited development Web server with the Run method.

When the server starts, it enters polling, waits, and processes the request. Polling continues until the program stops, such as by pressing the Ctrl-C key.

debug=TrueIndicates that debug mode is enabled. Convenient for us to debug.



Iyiming
Links: http://www.jianshu.com/p/303057e51417
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

The first knowledge of Python lightweight web framework Flask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.