Python web framework flask

Python has many web frameworks, which can be described as a hundred schools of thought contending. Here are a few frameworks that are more popular.

  • Django       has the highest market share, and the official documentation is almost perfect, but it is suitable for relatively large projects, and small projects will be cumbersome.
  • Tornado     is asynchronous, has high performance, provides more low-level details, and can also perform Web Sockets, but with databases….. (big pit)
  • Web.py is       small, refined, and doesn't have many extensions. The problem is that the author is so awesome that he was invited by God to have tea.
  • Flask           is young, appeared in 2010, extensible, small, built-in development server and debugger, uses jinja2 templates, fully compatible with WSGI 1.0

The flask framework is installed directly in pycharm. After testing the successful installation of flask, start the following:

Flask's simplest page

from flask import Flask #Import package 
 
app = Flask( __name__ ) #Create a web application 
 
@app.route( ' / ' ) #Define routes (Views), which can be understood as defining the URL of the page def index(): 
     return " Hello World " #Render the page
  
 
if __name__ == "__main__":
    app.run(host = ' 127.0.0.1 ' ,port=8080) #Run , specify the listening address as 127.0.0.1:8080

The result is as follows:

 

Click to enter the page to see "Hello World"

Enable Debug debugging

After modifying the code, we need to close and reopen the open Web program, which will affect the development efficiency and be annoying.

There's a function inside Flask specifically for something like this to happen.

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

debug parameter, this parameter defaults to False

After enabling this function, every time we modify the code, the web application will automatically adjust.

Now modify the code and save, the application will automatically update your code.

analyze

1  from flask import Flask   #Import package 
2  
3 app = Flask( __name__ )   #Create a web application 
4  
5 @app.route( ' / ' ) #Define   routes (Views), which can be understood as defining the URL of the page 6 def index( ):
 7 return " Hello World " #Rendering page 8 9 if __name__ == " __main__ " :
 10      app.run(host= ' 127.0.0.1 '
         
 
  , port=8080) #Run   , specify the listening address as 127.0.0.1:8080

Line 3: Flask expects an argument, which is usually the name of the main module or package. So usually  __name__ is passed in

Line 5: You can modify the configuration by using the app.config class to enable debug mode. Of course, the method used here can only be used when there are few settings, and several other methods of modifying the configuration will be introduced later.

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

Lines 8 - 9: Using the app.route() decorator saves the relationship between the URL and the executed view function (function index ) in the app.url_map property. This function is called when you visit the specified URL. When the first return is encountered, it will end. The return is your response

Line 12: Execute app.run to start the server. The default address Flask will listen on is 127.0.0.1:5000. When we specify the host and port parameters, the listening address is modified. After the service is started, it will first determine whether the parameters host and port are None. If they are None, the host and port will be modified to their default values. Then it will judge debug. Then werkzeug.serving.run_simple will be called to start the web service. By default, the single-process werkzeug.serving_BaseWSGIServer will be used to process client requests.

It should also be noted that the startup method of app.run here is only suitable for debugging while coding .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839904&siteId=291194637