Flask 读取 json 配置文件

import os
import json
from flask import Flask


def create_app():
    app = Flask('test')
    # 这里在虚拟环境中设置环境变量。 export RMON_CONFIG=xxx.json
    file = os.environ.get('RMON_CONFIG')
    content = ''
    if file:
        rest = {}
        with open(file) as f:
            for line in f:
                #  if line.strip().startswith('#'):
                if "#" in line:
                    continue
                content += line
    if content:
        config = json.loads(content)
        for k in config:
            app.config[k.upper()] = config[k]
    return app


if __name__ == '__main__':
    create_app()
标签:Flask 发布于:2019-11-04 00:47:28