3

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
blue-sky
  • 51,962
  • 152
  • 427
  • 752

2 Answers2

2

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

Greg Eremeev
  • 1,760
  • 5
  • 23
  • 33
  • please see question update. Is 'LOGGING' a property set in config file ? Using a custom property 'myvar' causes error. – blue-sky May 18 '18 at 18:53
  • @blue-sky you used from_pyfile. It means you have to provide a valid python file. Change your file content onto myvar="tester". In my example, variable LOGGING is just the valid variable, in terms of Python, which is located in Python module conf/api.conf. – Greg Eremeev May 18 '18 at 19:02
  • @blue-sky please, mark this answer as a right answer to show it other users – Greg Eremeev May 18 '18 at 19:22
  • please see further question update. I have not utilized 'LOGGING' correctly ? – blue-sky May 18 '18 at 19:37
  • @blue-sky You don't understand how to use Python correctly and SO. Don't update your question if this update changes the question completely. Try to read a doc about logging.config.dictConfig. You have to provide a dict! – Greg Eremeev May 18 '18 at 19:44
  • @blue-sky just delete the section with logging and it will work! You don't use logging in your example anyway. Return value from your config in 'hello_world' view if you wanna use it. – Greg Eremeev May 18 '18 at 19:49
0

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.

Jundiaius
  • 6,214
  • 3
  • 30
  • 43