from Flask import flask App = flask (__name) @app. Route ('/ ') def index (): return ' if __name__ = = '__main__': App.run ( Debug = True)
Then execute Pyhont hello.py in the command line and enter 127.0.0.1:5000/in the browser to see the word HelloWorld.
Code Analysis
First line
Import Flask Module
Second line
All flask programs must create a program instance, the program instance is an object of the Flask class, and all requests received from the client are forwarded to this object for processing, and the constructor of the Flask class has only one parameter that must be specified, that is, the name of the program's main module or package, in Python according to the call __ NAME__ modules are different, representing different values, if the module is imported, then __name__ is the name of the import module, if the pattern is executed directly, then the value of __name__ is __main__, Flask According to this parameter determine the root of the program to know where to find the module or static files
The third Five Elements
Use route()
the adorner to tell the URL of the Flask trigger function.
The function name can be used to generate the associated URL and return information that needs to be displayed in the user's browser.
The seven lines of the six
Finally, use the run()
function to run the local server and our app. if __name__ ==‘__main__‘:
make sure that the server runs only when you run the code with the Python interpreter, not when it is imported as a module.
Press CONTROL-C to stop the server.
Flask (i) a completed flask program