Getting started with Python flask basics

Source: Internet
Author: User

1. Hello world! in Flask

After creating a flask project using Pycharm, a flask app is created by default, where each line of code represents the meaning of the code in the

From flask Import Flask # import Flask Module   If no this module needs to use PIP install flask to install app = Flask (__name__) # Instantiate a flask instance, flask    Depending on the parameters passed in, determine the path of the application, the default path of the static file and the template file @app.route ('/') # applies the route part Def Hello_world ():    return ' Hello world! ' # This route points to the function part if __name__ = = ' __main__ ':    app.run () # Run Flask app

After running the program in the browser input http://localhost:5000 can see Hello world!, this is a simplest flask application, use CTRL + C to stop the service.

Attention:

1. When writing an application using flask, if the code is modified to require a manual restart service to take effect, flask has a debug mode to optimize the problem, and after the debugging support is started, not only will the service automatically restart after the code is modified, but the code will provide a useful debugger after an error has occurred. , debugging support allows code execution, there are security risks, and must not be used in production environments (two methods for adding debugging support: 1.app.debug=true;2.app.run (debug=true))

2. After starting the app with App.run (), the 127.0.0.1 5000 port is listened to by default, and if you want the external machine to access it only needs to include parameters in the Run method to specify the IP, port, thread process, etc. that the service is running. The Run method can add a lot of parameters and later continue.

2.Flask Routing

Flask's routing system is a @app.route decorator that can fill multiple parameters in this adorner, where the route decorator is to bind a view to the URL. For example:

# Use the routing system to bind different view functions to URL @app.route ('/') def hello_world ():    return ' Hello world! ' @app. Route ('/user ') def user ():    return ' Hello user! '

In addition, flask can dynamically construct a specific part of the URL, passing the dynamic value to the view function, for example:

@app. Route ('/user/<username> ') # receives a dynamic value passed to the View function, and the view function returns the dynamic graph data to the front End Def username (username):    return ' Hello { Username} '. Format (username=username) # You can also specify an optional converter that specifies the type of dynamic Value # example: <int:age># int accepts an integer # float accepts a floating-point # Path accepts a slash The string

ASDFASDF Asidifen SDFASDF

Getting started with Python flask basics

Related Article

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.