The Seven Year Itch.

MENU

Flask读取json配置文件

November 2, 2017 • Read: 412 • Python

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()
Tags: flask
Archives QR Code
QR Code for this page
Tipping QR Code