在Flask中将配置文件作为字典读取

在Flask中将配置文件作为字典读取

本文介绍了在Flask中将配置文件作为字典读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在/instance/app.cfg中,我已经配置了:

in /instance/app.cfg I've configured :

test=test

在我的烧瓶文件app.py中:

In my flask file app.py :

with app.open_instance_resource('app.cfg') as f:
    config = f.read()
    print('config' , type(config))

哪个会打印 config< class'bytes'>

阅读烧瓶文档时,它没有详细介绍如何从配置文件中读取值,这是如何实现的?

Reading the flask doc it does not detail how to read values from configuration files, how is this achieved ?

可以将配置读取为字典而不是字节吗?

can config be read a dictionary instead of bytes ?

更新:

app.py:

# Shamelessly copied from http://flask.pocoo.org/docs/quickstart/

from flask import Flask
app = Flask(__name__)

import os

ac = app.config.from_pyfile(os.path.join('.', 'conf/api.conf'), silent=True)
logging_configuration = app.config.get('LOGGING')
if ac:
    print(logging.config.dictConfig(ac))

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

api.conf:

myvar=tester

返回错误:

/./conf/api.conf", line 1, in <module>
    myvar=tester
NameError: name 'tester' is not defined

更新2:

app.py:

from flask import Flask
app = Flask(__name__)

import os
from logging.config import dictConfig

app.config.from_pyfile(os.path.join('.', 'conf/api.conf'), silent=True)

logging_configuration = app.config.get('LOGGING')
if logging_configuration:
    print(dictConfig(logging_configuration))

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

api.conf:

LOGGING="tester"

返回错误:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

推荐答案

您可以在烧瓶的文档中此处阅读相关内容>(标题为从文件配置")

You can read about it in flask's doc here (title "configuring-from-files")

open_instance_resource 只是处理位于实例文件夹"(可用于存储部署特定文件的特殊位置)中的文件的快捷方式.这不应该是将您的配置作为命令的一种方法.

open_instance_resource is only a shortcut to make deal with files which are located in "instance folder" (a special place where you can store deploy specific files). It's not supposed to be a way to get your config as a dict.

Flask将其配置变量(app.config)存储为dict对象.您可以通过一系列方法对其进行更新: from_envvar from_pyfile from_object 等.请查看源代码

Flask stores his config variable(app.config) as a dict object. You can update it via a bunch of methods: from_envvar, from_pyfile, from_object etc. Look at the source code

人们在基于烧瓶的应用程序中读取配置文件的典型方式之一:

One of the typical ways how people read config files in flask-based apps:

app = Flask('your_app')
...
app.config.from_pyfile(os.path.join(basedir, 'conf/api.conf'), silent=True)
...

之后,您可以根据需要使用类似dict的配置对象:

After that, you can use your dict-like config object as you want:

...
logging_configuration = app.config.get('LOGGING')
if logging_configuration:
    logging.config.dictConfig(logging_configuration)
...


从烧瓶导入烧瓶的


from flask import Flask
app = Flask(__name__)

import os

app.config.from_pyfile(os.path.join('.', 'conf/api.conf'), silent=True)

@app.route('/')
def hello_world():
    return 'Hello World! {}'.format(app.config.get('LOGGING'))

if __name__ == '__main__':
    app.run()

这篇关于在Flask中将配置文件作为字典读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:44