Python Web Framework Flask

Source: Internet
Author: User
Tags pycharm download

Python has a lot of web frameworks, which are the schools of contention, and I've got a few more frameworks here.

    • The Django market shares the highest, the official documents are almost perfect, but for larger projects, small projects can seem cumbersome.
    • Tornado can be asynchronous, high performance, provide more underlying details, but also web sockets, but with databases ..... (Tai Hang)
    • web.py Small, refining, expansion is not a lot, and the problem is that the author is too bull, God invited to tea.
    • Flask Young, 2010 appears, scalable, compact, built-in development server and debugger, using JINJA2 templates, fully compatible WSGI 1.0

Flask Frame installation directly within the Pycharm download is good, test flask after the installation is successful, start the following:

Flask One of the most simple pages

 fromFlaskImportFlask#Importing Packagesapp= Flask (__name__)#Create a web App@app. Route ('/')#define the route (views), which can be understood as the URL to define the pagedefindex ():return "Hello World" #Render Page if __name__=="__main__": App.run (Host='127.0.0.1', port=8080)#run, specify the listener address as 127.0.0.1:8080

The results are as follows:

Click on the page to see "Hello World"

turn on debug debugging

Modified code, we need to open the Web program to turn off and open, which will affect the development efficiency, but also annoying.

Flask has a feature inside, specifically for such things to happen.

if __name__ " __main__ " :    app.run (Host='127.0.0.1', port=8080,debug=true)

Debug parameter, this parameter is false by default

With this feature turned on, each time we modify the code, the Web app adjusts automatically.

Now that the code is modified and saved, the app will automatically update your code.

Analysis
1  fromFlaskImportFlask#Importing Packages2 3App = Flask (__name__)#Create a web App4 5@app. Route ('/')#define the route (views), which can be understood as the URL to define the page6 defindex ():7     return "Hello World"  #Render Page8 9 if __name__=="__main__":TenApp.run (host='127.0.0.1', port=8080)#run, specify the listener address as 127.0.0.1:8080

Line 3rd: Flask requires a parameter, which is usually the name of the main module or package. So it's usually passed __name__

Line 5th: You can modify the configuration by using the App. Config class to turn on debug mode. Of course, the method used here can only be used when the settings are very small, and later introduce several other ways to modify the configuration.

Line 7th: Remember I mentioned MVC before? Here is the V, which is the route. The purpose of the code here is to specify a route for us, that is, the address of the page.

Line 8–9: Using the app.route () Adorner saves the relationship of the URL and the executing view function (function Index) on the app.url_map property. This function is called when you access the specified URL. When the first return is encountered, it ends. The return of which is your response .

Line 12th: Execute app.run to start the server. The default flask will listen to the address is 127.0.0.1:5000. When we specify the host and port parameters, we modify the listening address. When the service starts, it will first determine if the parameter host and Port are none, and if none, the host and port will be modified to the default value. The debug is then judged. The werkzeug.serving.run_simple is then invoked to start the Web service, and the werkzeug.serving_basewsgiserver of a single process is used to process the client's request by default.

It is also important to note that the App.run start method here is only suitable for debugging in code.

Python 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.