I. First Hello World program
#Coding=utf-8 fromFlaskImportFlaskapp= Flask (__name__) @app. Route ('/')defHello_world ():return 'Hello world!'if __name__=='__main__': App.run (Debug=true)
Settings for 1.app parameters:
There are several ways to take the Debug mode example:
. Method one: Set the parameters to the App. Config dictionary:
app.config["DEBUG"] = True
. Mode two: Load file:
1. First create a config.py file in the root directory, and then write debug = True
2. Then set the parameters in the Include app file:
App.config.from_pyfile ("config.py")
. mode three: Load object (This is generally used in the actual development):
1. First create a config.py file in the root directory, and then write the following code:
Class Config (object): DEBUG = True
2. Import the Config class from the config file in the app file.
3. Then set:
App.config.from_object ("config")
. Mode four: App properties
App.debug = True
can also
App.run (Debug=true)
However, this method is different, but it can be set for debug mode only.
2. Take the value from the app. Config parameter:
1) app.config.get ("DEBUG")
2) Current_app.config.get ("DEBUG")
3) Config.debug
Two. Various ways to define routes
1. How to find routes:
Route converters built into the 2.flask (several forms of routing parameters)
A. The default type of system defaults is the string type, that is, when the parameters of the route match, the system defaults to match the parameters as string types. The following examples illustrate:
1@app. Route ('/<str>')2 def print_str (str):3 print Type (str) # Results for: <type ' Unicode ' >4 return Str5 6 7@app. Route ('/<string:str>')8 def print_str1 (str):9 print Type (str)# Result: <type ' Unicode ' >
10return Str
1@app. Route ('/<any (Hello,python,java, "javascript:;"):str>')2 def print_any (str): # can only match the data in parentheses, and the data inside the quotation marks can be freely written3 print Type (str)4 returnStr5 6 7@app. Route ('/<path:str>') # http://Localhost:5000/path/str8 def print_path (str):9 print Type (str)Ten returnSTR # results for: path/Str One A -@app. Route ('/<int:str>'# The data passed by the page (string type) is converted here directly to the int type . - def print_int (str): thePrint Type (str) # <type'int'> - returnstr (str) # must be converted to a string type at this time when returning data to the page - -@app. Route ('/<float:str>'# The decimal float converter only works if the page passes over it + def print_float1 (str): -Print Type (str) # <type'float'> + returnSTR (str)
B. The UUID is a 32-bit 16 decimal number.
3. Redirection of Routes
1) The redirection method is redirect () is often used in conjunction with Url_for. For example: Redirect (Url_for ("Method Name of view function"))
4. Two ways to return JSON data:
1) json.dumps ("dictionary")
2) jsonify ("dictionary")
The first method is simply to convert the dictionary to a string returned to the page, the response header'sContent-Type为text/html。
The second method is to convert the dictionary to JSON back to the page, the response header'sContent-Type为application/json。这是开发中比较常用的方法。
Now the need is to use the first method to achieve the effect of the second method, the code is:
return $, {'content-type'application/json'}
5. Custom Regular route Converters
1) Import Package
from werkzeug.routing import baseconverter
2) Follow Baseconverter to customize a class
class Regexconverter (baseconverter): def __init__ (Self,url_map,*args): super (Regexconverter,self ). __init__ (url_map) = args[0]
3) Add a custom class to the dictionary of the Route translator
app.url_map.converters["re" is the equivalent of using a class of your own definition to represent it with re.
4) Apply the regular route converter to the instance
@app. Route ('/<re (R ' \d{2} ") #:str>') # can only match two-bit digital Re (" regular ") ===> Equivalent to executing the Init method in Regexconverter, assigning a regular value to Self.regexdef print_str (str): return str
Flask Basics of the Python framework (i)