简体   繁体   English

通过 TOML 文件配置 Python Flask RESTplus 应用程序

[英]Configure Python Flask RESTplus app via TOML file

Based on the Configuration Handling Documents for Flask the section of Configuring from Files mentions a possibility to configure the App using files however it provides no example or mention of files that are not Python Files .根据Flask 的配置处理文档, 从文件配置部分提到了使用文件配置应用程序的可能性,但是它没有提供示例或提及不是Python 文件的文件

Is it possible to configure apps via files like config.yml or config.toml ?是否可以通过config.ymlconfig.toml等文件配置应用程序?

My Current flask app has configurations for two distinct databases and since I am using flask-restplus there are additional configurations for Swagger documentations.我当前的 flask 应用程序具有两个不同数据库的配置,由于我使用的是flask-restplus ,因此 Swagger 文档还有其他配置。

Snippet :片段

from flask import Flask

app = Flask(__name__)

def configure_app(flask_app):

    # MongoDB Setting
    flask_app.config['MONGO_URI'] = 'mongodb://user:password@mongo_db_endpoint:37018/myDB?authSource=admin'
    flask_app.config['MONGO_DBNAME'] = 'myDB'

    # InfluxDB Setting
    flask_app.config['INFLUXDB_HOST'] = 'my_influxdb_endpoint'
    flask_app.config['INFLUXDB_PORT'] = 8086
    flask_app.config['INFLUXDB_USER'] = 'influx_user'
    flask_app.config['INFLUXDB_PASSWORD'] = 'influx_password'
    flask_app.config['INFLUXDB_SSL'] =  True
    flask_app.config['INFLUXDB_VERIFY_SSL'] = False
    flask_app.config['INFLUXDB_DATABASE'] = 'IoTData'

    # Flask-Restplus Swagger Configuration
    flask_app.config['RESTPLUS_SWAGGER_UI_DOC_EXPANSION'] = 'list'
    flask_app.config['RESTPLUS_VALIDATE'] = True
    flask_app.config['RESTPLUS_MASK_SWAGGER'] = False
    flask_app.config['ERROR_404_HELP'] = False

def main():
    configure_app(app)

if __name__ == "__main__":

    main()

I would like to avoid setting large number of Environment Variables and wish to configure them using a config.toml file?我想避免设置大量环境变量并希望使用config.toml文件来配置它们?

How is this achieved in flask ?这是如何在flask中实现的?

You can use the .cfg files and from_envvar to achieve this.您可以使用.cfg文件和from_envvar来实现此目的。 Create config file with all your environment variables.使用所有环境变量创建配置文件。

my_config.cfg我的配置文件

MONGO_URI=mongodb://user:password@mongo_db_endpoint:37018
..
..
ERROR_404_HELP=False

Then set the env var APP_ENVS=my_config.cfg .然后设置APP_ENVS=my_config.cfg Now all you need to do is use from_envvars given by Flask.现在您需要做的就是使用 Flask 给出的 from_envvars。

def configure_app(flask_app):
    flask_app.config.from_envvar('APP_ENVS')
    # configure any other things
    # register blue prints if you have any

Quoting from documentation :引用文档

Configuring from Data Files从数据文件配置

It is also possible to load configuration from a file in a format of your choice using from_file() .也可以使用from_file()以您选择的格式从文件加载配置。 For example to load from a TOML file:例如从 TOML 文件加载:

 import toml app.config.from_file("config.toml", load=toml.load)

Or from a JSON file:或者来自 JSON 文件:

 import json app.config.from_file("config.json", load=json.load)

EDIT: The above feature is new for v2.0.编辑:上述功能是 v2.0 的新功能。

Link to the documentation reference:链接到文档参考:

Class Flask.config , method from_file(filename, load, silent=False) Class Flask.config ,方法from_file(filename, load, silent=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM