blue-sky
blue-sky

Reputation: 53826

Reading config file as dictionary in Flask

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

test=test

In my flask file app.py :

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

Which prints 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 ?

Update :

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

returns error :

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

Update 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"

returns error :

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

Upvotes: 3

Views: 5552

Answers (2)

Greg Eremeev
Greg Eremeev

Reputation: 1840

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

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

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 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)
...

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()

Upvotes: 2

Jundiaius
Jundiaius

Reputation: 7630

If you do app.config.from_pyfile('app.cfg') you can obtain your config as a dictionary by dict(app.config).

However, this dictionary will contain the whole configuration for your app not only those variables which were set by the configuration file.

Upvotes: 0

Related Questions