flask config and circular imports
- From:
- Jérôme Pigeot
- Date:
- 2011-03-03 @ 14:14
Hello,
I need to use an external configuration file in a Flask app:
the structure looks like :
manage.py
myapp/
myapp/__init__.py
myapp/database.py
myapp/models.py
myapp/default_config.py
myapp/views/__init__.py
myapp/views/myview.py
---------------------------------------------
here's the __init__.py file:
from flask import Flask
from myapp.views.myview import myview
from myapp.database import db_session
app = Flask(__name__)
app.config.from_object('myapp.default_settings')
app.config.from_envvar('MY_SETTINGS')
app.register_module(myview)
@app.after_request
def shutdown_session(response):
db_session.remove()
return response
---------------------------------------------------
here's the database.py file:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mydatabaseaccesses')
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
---------------------------------------------------------------
here's a part of the models.py file:
from sqlalchemy import Column, Integer, Unicode
from myapp.database import Base
class MyModel(Base):
__tablename__ = 'mymodel'
id_ = Column(Integer, primary_key=True)
name = Column(Unicode)
....
---------------------------------------------------------------
finally a part of my default_config.py file:
SECRET_KEY = 'mysupersecretkey'
DEBUG = True
DB_URI = 'a_sql_alchemy_db_uri'
...
-----------------------------------------------------------------
I would like to manage my database access from my config file (default
config in the default_config.py, and wanted one in the MY_SETTINGS envvar)
like
mention the flask configuration (http://flask.pocoo.org/docs/config/)
My problem is how to access to the configuration in the database.py file to
use the DB_URI defined in the config object????
I read a lot of exemples in the mailing list but i was unable to success
doing it...
Thanks by advance for any tip.
Jérôme.
Re: [flask] flask config and circular imports
- From:
- Andrew Wilson
- Date:
- 2011-03-03 @ 17:40
2011/3/3 Jérôme Pigeot <j.pigeot@gmail.com>
> Hello,
>
> I need to use an external configuration file in a Flask app:
>
> My problem is how to access to the configuration in the database.py file to
> use the DB_URI defined in the config object????
> I read a lot of exemples in the mailing list but i was unable to success
> doing it...
>
> Thanks by advance for any tip.
>
> Jérôme.
>
>
Circular imports happen such that if you are in moduleA and import moduleB,
and moduleB imports objectA from moduleA, then objectA is available to
moduleB AS IT EXISTS at the time that moduleB was imported. So a quick and
dirty not-so-PEP 8 way to do it is to do your config, then import/do
everything else. This should work for your purposes but it is not perfect
since the app you import isn't the fully realized app object. If you keep
things simple and only using app other places to grab things from
app.config, then you should be fine.
So doing this in your code would look something like this:
here's the __init__.py file:
from flask import Flask
# create the app object and set up config
app = Flask(__name__)
app.config.from_object('myapp.default_settings')
app.config.from_envvar('MY_SETTINGS')
# now do everything else...
from myapp.views.myview import myview
from myapp.database import db_session
app.register_module(myview)
@app.after_request
def shutdown_session(response):
db_session.remove()
return response
...now in database.py you can import app and app.config should be available
Re: [flask] flask config and circular imports
- From:
- Jérôme Pigeot
- Date:
- 2011-03-03 @ 19:53
It works fine : thanks Andew!!!
It must be very common needing some config variables in flask modules isn'it
??
Do you use another simple ways to deal with that??
2011/3/3 Andrew Wilson <wilson.andrew.j@gmail.com>
>
>
> 2011/3/3 Jérôme Pigeot <j.pigeot@gmail.com>
>
>> Hello,
>>
>> I need to use an external configuration file in a Flask app:
>>
>> My problem is how to access to the configuration in the database.py file
>> to use the DB_URI defined in the config object????
>> I read a lot of exemples in the mailing list but i was unable to success
>> doing it...
>>
>> Thanks by advance for any tip.
>>
>> Jérôme.
>>
>>
>
>
>
> Circular imports happen such that if you are in moduleA and import moduleB,
> and moduleB imports objectA from moduleA, then objectA is available to
> moduleB AS IT EXISTS at the time that moduleB was imported. So a quick and
> dirty not-so-PEP 8 way to do it is to do your config, then import/do
> everything else. This should work for your purposes but it is not perfect
> since the app you import isn't the fully realized app object. If you keep
> things simple and only using app other places to grab things from
> app.config, then you should be fine.
>
>
> So doing this in your code would look something like this:
>
>
>
> here's the __init__.py file:
>
> from flask import Flask
>
> # create the app object and set up config
> app = Flask(__name__)
> app.config.from_object('myapp.default_settings')
> app.config.from_envvar('MY_SETTINGS')
>
>
> # now do everything else...
> from myapp.views.myview import myview
> from myapp.database import db_session
>
> app.register_module(myview)
>
> @app.after_request
> def shutdown_session(response):
> db_session.remove()
> return response
>
>
>
> ...now in database.py you can import app and app.config should be available
>
Re: [flask] flask config and circular imports
- From:
- Andy Wilson
- Date:
- 2011-03-04 @ 15:56
This is pretty much how I do it.
2011/3/3 Jérôme Pigeot <j.pigeot@gmail.com>
> It works fine : thanks Andew!!!
> It must be very common needing some config variables in flask modules
> isn'it ??
> Do you use another simple ways to deal with that??
>
> 2011/3/3 Andrew Wilson <wilson.andrew.j@gmail.com>
>
>
>>
>> 2011/3/3 Jérôme Pigeot <j.pigeot@gmail.com>
>>
>>> Hello,
>>>
>>> I need to use an external configuration file in a Flask app:
>>>
>>> My problem is how to access to the configuration in the database.py file
>>> to use the DB_URI defined in the config object????
>>> I read a lot of exemples in the mailing list but i was unable to success
>>> doing it...
>>>
>>> Thanks by advance for any tip.
>>>
>>> Jérôme.
>>>
>>>
>>
>>
>>
>> Circular imports happen such that if you are in moduleA and import
>> moduleB, and moduleB imports objectA from moduleA, then objectA is available
>> to moduleB AS IT EXISTS at the time that moduleB was imported. So a quick
>> and dirty not-so-PEP 8 way to do it is to do your config, then import/do
>> everything else. This should work for your purposes but it is not perfect
>> since the app you import isn't the fully realized app object. If you keep
>> things simple and only using app other places to grab things from
>> app.config, then you should be fine.
>>
>>
>> So doing this in your code would look something like this:
>>
>>
>>
>> here's the __init__.py file:
>>
>> from flask import Flask
>>
>> # create the app object and set up config
>> app = Flask(__name__)
>> app.config.from_object('myapp.default_settings')
>> app.config.from_envvar('MY_SETTINGS')
>>
>>
>> # now do everything else...
>> from myapp.views.myview import myview
>> from myapp.database import db_session
>>
>> app.register_module(myview)
>>
>> @app.after_request
>> def shutdown_session(response):
>> db_session.remove()
>> return response
>>
>>
>>
>> ...now in database.py you can import app and app.config should be
>> available
>>
>
>