librelist archives

« back to archive

Adding some new global configuration

Adding some new global configuration

From:
Armando Neto
Date:
2011-03-21 @ 15:04
Hi, this is my project: https://github.com/netoarmando/tccweb

I'm using this approach to use configuration:

import tccweb.settings


def create_app():

    app = Flask(__name__)
    app.config.from_object(settings.DevelopmentConfig)

    app.register_module(admin)


    return app

 on settings.py:

class Config(object):
    SECRET_KEY = '\xa3Lc}\xf4\xa8\x8e
\xd4\x1c,I2\xb4j*3e\xbc\xa9\x03\xca\x7f\xe1'
    DEBUG = False
    TESTING = False
    SQLALCHEMY_ECHO = False

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@localhost/mydatabase'

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/database-development.db' %
os.path.dirname(__file__)

class TestinConfig(Config):

    TESTING = True

What I want to know is how a module can acess a new Constraint that I could
set in here?
For an example:

class Config(object):
    UPLOAD_FOLDER = '/temp'

Re: [flask] Adding some new global configuration

From:
Ron DuPlain
Date:
2011-03-21 @ 15:12
Hi,

On Mon, Mar 21, 2011 at 11:04 AM, Armando Neto <neto.armando@gmail.com> wrote:
> What I want to know is how a module can acess a new Constraint that I could
> set in here?
> For an example:
>
> class Config(object):
>     UPLOAD_FOLDER = '/temp'

If I understand your question, you'd like to access config directives
in your code.  Once your app is initialized:

app = Flask(__name__)
app.config.from_object(config) # whatever your config obj is

You can:

app.config['UPLOAD_FOLDER']

where app.config is a dictionary (specifically a subclass of dict).
You can use all dict methods in your code.

-Ron

Re: [flask] Adding some new global configuration

From:
Ishbir Singh
Date:
2011-03-21 @ 15:10
Hey,

You can access it simply by this-

from flask import current_app

conf_value = current_app.config['UPLOAD_FOLDER']

On Mon, Mar 21, 2011 at 8:34 PM, Armando Neto <neto.armando@gmail.com>wrote:

> Hi, this is my project: https://github.com/netoarmando/tccweb
>
> I'm using this approach to use configuration:
>
> import tccweb.settings
>
>
>
>
> def create_app():
>
>
>     app = Flask(__name__)
>     app.config.from_object(settings.DevelopmentConfig)
>
>
>
>     app.register_module(admin)
>
>
>
>     return app
>
>  on settings.py:
>
>
> class Config(object):
>     SECRET_KEY = '\xa3Lc}\xf4\xa8\x8e 
\xd4\x1c,I2\xb4j*3e\xbc\xa9\x03\xca\x7f\xe1'
>
>
>     DEBUG = False
>     TESTING = False
>
>     SQLALCHEMY_ECHO = False
>
> class ProductionConfig(Config):
>
>     SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@localhost/mydatabase'
>
>
> class DevelopmentConfig(Config):
>     DEBUG = True
>
>     SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/database-development.db' % 
os.path.dirname(__file__)
>
>
>
> class TestinConfig(Config):
>
>
>     TESTING = True
>
> What I want to know is how a module can acess a new Constraint that I could
> set in here?
> For an example:
>
> class Config(object):
>     UPLOAD_FOLDER = '/temp'
>
>
>


-- 
Ishbir Singh