Flask (i) a completed flask program

Source: Internet
Author: User

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

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.