Seven lines of code to implement a flask app
fromimport= Flask(__name__)@app.route(‘/‘)def helloworld(): return‘helloworld‘if__name__==‘__main__‘: app.run()
app.run()
Suitable for commissioning only, not for production environments, production environments should start with Gunicorn and Uwsgi
Configuration Management
- App. Config is an instance of the Flask.config.Config class that inherits from the Python built-in data structure dict, which you can add with Dict configuration
-
app.config[' DEBUG ']= True
- Use the Update method
App.config.update (Debug= true , Secrey_key= ' HELLOWORLD ' ,)
General project configurations are uniformly placed in a single settings.py file that can centrally manage configuration items
# settings.pyclass Develop(): =Trueclass Product(): =False== Product()
# app.pyfromimport Flaskfromimport= Flask(__name__)app.config.from_object(product)
- By loading the file name, you can learn
app.config.from_pyfile(‘settings.py‘,slient=True)
- By loading the environment variables, you can understand
app.config.from_envvar(‘settings_path‘)
Configuration of dynamic URLs
Custom URL Converters
-
Inherits from Baseconverter
From werkzeug.routing import baseconverterclass Customconverter (baseconverter): def __init__ ( Self , Url_map,regex): super (customconverter, self ). __init__ (url_map) self . Regex = regex
pre>
Use
fromimport= Flask(__name__)app.config.from_object(develop)app.url_map.converters[‘re‘=# 添加到converter列表中@app.route(‘/hello/<re("[1-9]{2}"):cus>/‘# re("regex") 定义匹配规则def custom(cus): return cus
Flask Learning configuration file loading and use of dynamic URLs