When using module organization, what's the best practice for accessing configuration within those modules? My __init__.py has this line: app.config.from_object(settings) That pulls from a few config variables from a settings.py. But I want to either pull my database name from that file in all of the separate view modules, or define a DB connection method that all of the views can use. I can't seem to import the config dict or a connect_db() method in __init__.py into the views. How should I go about this?
The best way, IMO, would be to define some variables in the settings.py file.. like- DB_NAME = 'db' DB_USERNAME = 'user' and so on.. Then, in __init__.py, after importing the config, connect to the database, something like- db_conn = connect_db(app.config['DB_NAME'], ...) In your views, import it like- from myapp import db_conn Then do whatever you want.. On Sun, Feb 13, 2011 at 1:16 PM, Aaron Kavlie <akavlie@gmail.com> wrote: > When using module organization, what's the best practice for accessing > configuration within those modules? > > My __init__.py has this line: > app.config.from_object(settings) > > That pulls from a few config variables from a settings.py. > > But I want to either pull my database name from that file in all of the > separate view modules, or define a DB connection method that all of the > views can use. I can't seem to import the config dict or a connect_db() > method in __init__.py into the views. > > How should I go about this? > -- Ishbir Singh
The weird thing is, within my views I can't seem to import ANYTHING that I
place in my __init__.py.
I have a structure like this (not all files included):
/kinderflask
runserver.py
/apps
__init__.py
forms.py
/static
/templates
/views
root.py
cal.py
courses.py
If I put something like "foo = 42" in my /apps/__init__.py and try "from
apps import foo" in a view, I get the error "Cannot import name foo".
Whereas "from apps.forms import FooForm" works just fine.
On Sun, Feb 13, 2011 at 7:14 AM, Ishbir Singh <ishbir24@gmail.com> wrote:
> The best way, IMO, would be to define some variables in the settings.py
> file.. like-
> DB_NAME = 'db'
> DB_USERNAME = 'user'
> and so on.. Then, in __init__.py, after importing the config, connect to
> the database, something like-
>
> db_conn = connect_db(app.config['DB_NAME'], ...)
>
> In your views, import it like-
>
> from myapp import db_conn
>
> Then do whatever you want..
>
> On Sun, Feb 13, 2011 at 1:16 PM, Aaron Kavlie <akavlie@gmail.com> wrote:
>
>> When using module organization, what's the best practice for accessing
>> configuration within those modules?
>>
>> My __init__.py has this line:
>> app.config.from_object(settings)
>>
>> That pulls from a few config variables from a settings.py.
>>
>> But I want to either pull my database name from that file in all of the
>> separate view modules, or define a DB connection method that all of the
>> views can use. I can't seem to import the config dict or a connect_db()
>> method in __init__.py into the views.
>>
>> How should I go about this?
>>
>
>
>
> --
> Ishbir Singh
>
--
freelance web development
aaron.kavlie.net
On Mon, Feb 14, 2011 at 1:28 PM, Aaron Kavlie <akavlie@gmail.com> wrote: > The weird thing is, within my views I can't seem to import ANYTHING that I > place in my __init__.py. > I have a structure like this (not all files included): > /kinderflask > runserver.py > /apps > __init__.py > forms.py > /static > /templates > /views > root.py > cal.py > courses.py > If I put something like "foo = 42" in my /apps/__init__.py and try "from > apps import foo" in a view, I get the error "Cannot import name foo". > Whereas "from apps.forms import FooForm" works just fine. Looks like a circular import in your Python code. When I want to access the main app config, I use: from flask import current_app as app app.config['SQLALCHEMY_DATABASE_URI'] flask.current_app is a proxy to the app in the current request context. See: http://flask.pocoo.org/docs/api/#flask.current_app -Ron > On Sun, Feb 13, 2011 at 7:14 AM, Ishbir Singh <ishbir24@gmail.com> wrote: >> >> The best way, IMO, would be to define some variables in the settings.py >> file.. like- >> DB_NAME = 'db' >> DB_USERNAME = 'user' >> and so on.. Then, in __init__.py, after importing the config, connect to >> the database, something like- >> db_conn = connect_db(app.config['DB_NAME'], ...) >> In your views, import it like- >> from myapp import db_conn >> Then do whatever you want.. >> On Sun, Feb 13, 2011 at 1:16 PM, Aaron Kavlie <akavlie@gmail.com> wrote: >>> >>> When using module organization, what's the best practice for accessing >>> configuration within those modules? >>> My __init__.py has this line: >>> app.config.from_object(settings) >>> That pulls from a few config variables from a settings.py. >>> But I want to either pull my database name from that file in all of the >>> separate view modules, or define a DB connection method that all of the >>> views can use. I can't seem to import the config dict or a connect_db() >>> method in __init__.py into the views. >>> How should I go about this? >> >> >> -- >> Ishbir Singh > > > > -- > freelance web development > aaron.kavlie.net >
Thanks for the help guys. A bit of background: I ran into this due to the need to use a different database for unit testing. My solution for this was to rewrite a DATABASE variable in app.config (similar to the unit test examples for flaskr), but this caused problems as mentioned earlier in this thread -- I was connecting to the DB in every view. What I didn't realize, though, was that I could simply do an @app.before_request method in __init__.py to cover all views (I was doing @root.before_request, etc. and mostly repeating the same code in all views). With that change, overwriting the config value in the test looks to be working properly. If I do need to reference a config variable in a view in the future, I'll look to that current_app solution in the future. I'm not sure it will be necessary though. On Mon, Feb 14, 2011 at 3:02 PM, Ron DuPlain <ron.duplain@gmail.com> wrote: > On Mon, Feb 14, 2011 at 1:28 PM, Aaron Kavlie <akavlie@gmail.com> wrote: > > The weird thing is, within my views I can't seem to import ANYTHING that > I > > place in my __init__.py. > > I have a structure like this (not all files included): > > /kinderflask > > runserver.py > > /apps > > __init__.py > > forms.py > > /static > > /templates > > /views > > root.py > > cal.py > > courses.py > > If I put something like "foo = 42" in my /apps/__init__.py and try "from > > apps import foo" in a view, I get the error "Cannot import name foo". > > Whereas "from apps.forms import FooForm" works just fine. > > Looks like a circular import in your Python code. > When I want to access the main app config, I use: > > from flask import current_app as app > app.config['SQLALCHEMY_DATABASE_URI'] > > flask.current_app is a proxy to the app in the current request context. > See: http://flask.pocoo.org/docs/api/#flask.current_app > > -Ron > > > > On Sun, Feb 13, 2011 at 7:14 AM, Ishbir Singh <ishbir24@gmail.com> > wrote: > >> > >> The best way, IMO, would be to define some variables in the settings.py > >> file.. like- > >> DB_NAME = 'db' > >> DB_USERNAME = 'user' > >> and so on.. Then, in __init__.py, after importing the config, connect to > >> the database, something like- > >> db_conn = connect_db(app.config['DB_NAME'], ...) > >> In your views, import it like- > >> from myapp import db_conn > >> Then do whatever you want.. > >> On Sun, Feb 13, 2011 at 1:16 PM, Aaron Kavlie <akavlie@gmail.com> > wrote: > >>> > >>> When using module organization, what's the best practice for accessing > >>> configuration within those modules? > >>> My __init__.py has this line: > >>> app.config.from_object(settings) > >>> That pulls from a few config variables from a settings.py. > >>> But I want to either pull my database name from that file in all of the > >>> separate view modules, or define a DB connection method that all of the > >>> views can use. I can't seem to import the config dict or a connect_db() > >>> method in __init__.py into the views. > >>> How should I go about this? > >> > >> > >> -- > >> Ishbir Singh > > > > > > > > -- > > freelance web development > > aaron.kavlie.net > > > -- freelance web development aaron.kavlie.net