Site backend _python+flask.0004.flask Configuration management three ways to load the external configuration?

Source: Internet
Author: User
Tags throw exception

Brief introduction:

Description: Complex projects need to configure a variety of environments, if the settings can be directly hard-coded, set the number of words can be loaded by loading configuration/load file/load variable way to set

App.config.update (Debug=true,)

extension: App. Config is an instance of the Flask.config.Config class, inheriting the built-in data structure dict of the child py, so you can use the above Update method to support incoming multiple key-value pairs, in fact, App. Config has many configuration variables ( http://flask.pocoo.org/docs/0.11/config/#Builtin%20configuration%20values), And app. Config supports a variety of ways to update configurations

Note: Regardless of the configuration method used, the key name in the configuration must be uppercase, otherwise it will not be recognized loading, please note ~


Load configuration:

Description: Supports loading configuration via the configuration file module name or the imported module object

# Mode one: app.config.from_object (' config ') is passed as a string parameter and will automatically load the config.py file in the root directory and will load only variables in the file that are named uppercase

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "from __future__ Import absolute_import# Description: Import Public Module # Description: Import other modules author = ' Limanman '
#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from flask import flask# Description: Import other modules app = Flask (__name__) app.config.from_object (' config ') @app. Route ('/') Def hello_word (): Return App.config.get (' AUTHOR ') if __name__ = = ' __main__ ': App.run (host= ' 0.0.0.0 ', port=9000)

# mode two: App.config.from_object (config) is automatically loaded when passed as an object, and only the property named uppercase in the object is loaded, and since it is the object then it is possible to pass the class.

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "from __future__ Import absolute_import# Description: Import Public Module # Description: Import other modules author = ' Limanman '
#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module Imports Configfrom flask Import flask# Description: Import other modules app = Flask (__name__) app.config.from_object (config) @ App.route ('/') def Hello_word (): Return App.config.get (' AUTHOR ') if __name__ = = ' __main__ ': App.run (host= ' 0.0.0.0 ', PO rt=9000)


Load file:

Note: Support is loaded by file name, and the suffix name is not limited to. PY, default if the file does not exist will throw an exception, you can use Silent=true set not throw exception

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "from __future__ Import absolute_import# Description: Import Public Module # Description: Import other modules author = ' Limanman '
#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from flask import flask# Description: Import other modules app = Flask (__name__) app.config.from_pyfile (' config.py ', silent=true) @ App.route ('/') def Hello_word (): Return App.config.get (' AUTHOR ') if __name__ = = ' __main__ ': App.run (host= ' 0.0.0.0 ', PO rt=9000)


Load variables:

Description: Support through environment variable loading, default if file does not exist will throw exception, can use Silent=true set not throw exception

Set config=config.py

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from flask import flask# Description: Import other modules app = Flask (__name__) app.config.from_envvar (' config ', silent=true) @ App.route ('/') def Hello_word (): Return App.config.get (' AUTHOR ') or ' ERROR ' if __name__ = = ' __main__ ': App.run (host= ' 0 .0.0.0 ', port=9000)

Note: Especially on Windows set config=config.py, do not owe config.py quotes, and the view function must remember to return a response object, because none cannot generate a response object, So make sure you're not returning none.


Common ways:

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:#  "" "from __future__ Import absolute_import# Description: Import Public Module # Description: Import other Modules # Description: Configure the base class classes Class __config (object): AUTHOR = ' Limanman ' # Description: Development Environment Class __devsconfig (__config): pass# Description: Forecast Environment Class __testconfig (__config): pass# Description: Formal Environment class __prodconfig (     __config): pass# Description: Load Config On demand = {' default ': __devsconfig, ' develop ': __testconfig, ' testing ': __prodconfig, ' Product ': __prodconfig,}

Description: As above is the most common way in my project, when loading the configuration file externally through the From Config Import config to import the development environment/pre-test environment/formal environment different configuration, through App.config.from_object (config[' Default ']) to load the configuration



This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1865619

Site backend _python+flask.0004.flask Configuration management three ways to load the external configuration?

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.