Flask use to build a Web application

Flask is a lightweight Web application framework written in Python using.

First, install Flask

As an administrator, open a command prompt, enter the following command

py -3 -m pip install flask

This command will connect to the website to download and install the PyPI Flask module, and several modules dependent Flask: Werkzeug, MarkupSafe, Jinja2, itsdangerous , click.
After installation is complete, the last part of the command prompt window you can see:

Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, click, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0

Second, create a Web application a minimum of hello world

1, create a hello.py, reads as follows

# Import module flask Flask class 
from flask Import Flask 

# the __name__ name of the current event module 
App = Flask ( the __name__ ) 

# Set Routing 
@ app.route ( ' / ' ) 
 DEF Hello () -> STR:
      return  ' the Hello World ! ' 

"" " RUN () there are three optional parameter 
host host, the default is 127.0.0.1 
port port number, default 5000 
debug debugging mode is turned on, the default is False, modify the code as it is set to True will automatically reboot 
" " " 
app.run ()

In the command prompt window, navigate to the directory where hello.py, and execute the command py -3 hello.py

D:\projects\python>py -3 hello.py
 * Serving Flask app "hello" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

It said that it has launched a Web application, debug mode is not enabled, address http://127.0.0.1:5000, press CTRL + C to exit the Web application.

3, in the browser enter http://127.0.0.1:5000
can see the browser displays Hello World!
At the same time just a command prompt window you can see
127.0.0.1 - - [23 / Jun / 2019 09:31:55 ] "GET / HTTP / 1.1" 200 -

Third, routing configuration

Establish links between the URL and the operation is called routing function.
Routing supports dynamic name rules are as follows:

1. <id>: The default type is accepted STR 
2. <String: id>: id of the specified type str, can not contain path separator / 
3. <int: id>: id specified type is an integer of 
4. < float: id>: id designated to the floating point type (rounded, and the integer type can not be received) 
5. the <path: path1>: Specifies the path is received by any non-empty string, may comprise path separators /

Use examples:

from flask import Flask

app = Flask(__name__) 


@app.route('/getStr/<id>') 
def getStr(id) -> str:
     """匹配str类型的id值,如/getStr/1 """
     return 'id: %s' %id

@app.route('/getInt/<int:id>') 
def getInt(id) -> str:
     """匹配int类型的id值,如/getInt/1 """
     return 'id: %d' %id

@app.route('/ getFloat / <float: ID> ' ) 
 DEF getFloat (ID) -> STR:
      "" " ID matches the value of type float, such as /getFloat/1.053 " "" 
     return  ' ID:% .2f ' % ID 

@app. route ( ' / getPath / <path: path> ' ) 
 DEF getPath (path) -> STR:
      "" " matches non-blank character, generally matching url path value, such as / getPath / Test /. 1 " "" 
     return  ' path: S% ' % path 

app.run (Debug = True)

 

Guess you like

Origin www.cnblogs.com/gdjlc/p/11072118.html