librelist archives

« back to archive

How to refer main app's variable in app's modules?

How to refer main app's variable in app's modules?

From:
Tomek Sz.
Date:
2015-02-23 @ 11:46
Hello,

I've just joined mailing list, so hello everyone!

I tried to find an answer for below question on Stack, but probably this 
is right place for it.

Structure of my Flask app (/env is virtual env):

|/env
/env/bin/...
/env/include/...
/env/lib/...
/env/lib64/...
/env/myapp.py
/mymodules/__init__.py
/mymodules/users/__init__.py
/mymodules/users/controller.py|

myapp.py has below content:

|# -*- coding: utf-8 -*-

from  flaskimport  Flask
from  flaskimport  request,  current_app
from  flask.ext.sqlalchemyimport  SQLAlchemy
import  mymodules.users.controlleras  MyMods


app=  Flask(__name__)

db=  SQLAlchemy(app)

# [...]

@app.route(u"/test",  methods=['GET'])
def  test():

     return  MyMods.mytest()

if  __name__==  u"__main__":
     app.run()|

Content of controller.py

|# -*- coding: utf-8 -*-

def  mytest():
     return  "ok"|

Question: how can I use |app| and |db| inside controller.py?

I can pass it as param:

|MyMods.mytest(app,  db)

def  mytest(app_ref,  db_ref):|

but is it the right way (amongst Flask developers) of doing it?


--
Tomasz Sz.


Re: [flask] How to refer main app's variable in app's modules?

From:
Dorian Hoxha
Date:
2015-02-23 @ 15:51
Probably current_app:

http://flask.pocoo.org/docs/0.10/api/#flask.current_app

On Mon, Feb 23, 2015 at 12:46 PM, Tomek Sz. <pideving@gmail.com> wrote:

>  Hello,
>
> I've just joined mailing list, so hello everyone!
>
> I tried to find an answer for below question on Stack, but probably this
> is right place for it.
>
> Structure of my Flask app (/env is virtual env):
>
> 
/env/env/bin/.../env/include/.../env/lib/.../env/lib64/.../env/myapp.py/mymodules/__init__.py/mymodules/users/__init__.py/mymodules/users/controller.py
>
> myapp.py has below content:
>
> # -*- coding: utf-8 -*-
> from flask import Flaskfrom flask import request, current_appfrom 
flask.ext.sqlalchemy import SQLAlchemyimport mymodules.users.controller as
MyMods
>
>
> app = Flask(__name__)
>
> db = SQLAlchemy(app)
> # [...]
> @app.route(u"/test", methods=['GET'])def test():
>
>     return MyMods.mytest()
> if __name__ == u"__main__":
>     app.run()
>
> Content of controller.py
>
> # -*- coding: utf-8 -*-
> def mytest():
>     return "ok"
>
> Question: how can I use app and db inside controller.py?
>
> I can pass it as param:
>
> MyMods.mytest(app, db)
> def mytest(app_ref, db_ref):
>
> but is it the right way (amongst Flask developers) of doing it?
>
>
> --
> Tomasz Sz.
>
>
>
>

Re: [flask] How to refer main app's variable in app's modules?

From:
Tomek Sz.
Date:
2015-02-26 @ 12:23
Ok, thank you!

How can I access db variable using this technique?


On 02/23/2015 04:51 PM, Dorian Hoxha wrote:
> Probably current_app:
>
> http://flask.pocoo.org/docs/0.10/api/#flask.current_app
>
> On Mon, Feb 23, 2015 at 12:46 PM, Tomek Sz. <pideving@gmail.com 
> <mailto:pideving@gmail.com>> wrote:
>
>     Hello,
>
>     I've just joined mailing list, so hello everyone!
>
>     I tried to find an answer for below question on Stack, but
>     probably this is right place for it.
>
>     Structure of my Flask app (/env is virtual env):
>
>     |/env
>     /env/bin/...
>     /env/include/...
>     /env/lib/...
>     /env/lib64/...
>     /env/myapp.py
>     /mymodules/__init__.py
>     /mymodules/users/__init__.py
>     /mymodules/users/controller.py|
>
>     myapp.py has below content:
>
>     |# -*- coding: utf-8 -*-
>
>     from  flaskimport  Flask
>     from  flaskimport  request,  current_app
>     from  flask.ext.sqlalchemyimport  SQLAlchemy
>     import  mymodules.users.controlleras  MyMods
>
>
>     app=  Flask(__name__)
>
>     db=  SQLAlchemy(app)
>
>     # [...]
>
>     @app.route(u"/test",  methods=['GET'])
>     def  test():
>
>          return  MyMods.mytest()
>
>     if  __name__==  u"__main__":
>          app.run()|
>
>     Content of controller.py
>
>     |# -*- coding: utf-8 -*-
>
>     def  mytest():
>          return  "ok"|
>
>     Question: how can I use |app| and |db| inside controller.py?
>
>     I can pass it as param:
>
>     |MyMods.mytest(app,  db)
>
>     def  mytest(app_ref,  db_ref):|
>
>     but is it the right way (amongst Flask developers) of doing it?
>
>
>     --
>     Tomasz Sz.
>
>
>
>

Re: [flask] How to refer main app's variable in app's modules?

From:
Dorian Hoxha
Date:
2015-02-26 @ 18:16
I usually store the 'db' variable in __init__.py and import it on other
modules with:
from . import db

And then to run the app, create a 'run.py' outside the directory and do:
from app import app
app.run(debug=True)

Assuming the __init__.py that has the db is inside a folder named 'app'.

This will help you with many questions:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

On Thu, Feb 26, 2015 at 1:23 PM, Tomek Sz. <pideving@gmail.com> wrote:

>  Ok, thank you!
>
> How can I access db variable using this technique?
>
>
>
> On 02/23/2015 04:51 PM, Dorian Hoxha wrote:
>
> Probably current_app:
>
> http://flask.pocoo.org/docs/0.10/api/#flask.current_app
>
> On Mon, Feb 23, 2015 at 12:46 PM, Tomek Sz. <pideving@gmail.com> wrote:
>
>>  Hello,
>>
>> I've just joined mailing list, so hello everyone!
>>
>> I tried to find an answer for below question on Stack, but probably this
>> is right place for it.
>>
>> Structure of my Flask app (/env is virtual env):
>>
>> 
/env/env/bin/.../env/include/.../env/lib/.../env/lib64/.../env/myapp.py/mymodules/__init__.py/mymodules/users/__init__.py/mymodules/users/controller.py
>>
>> myapp.py has below content:
>>
>> # -*- coding: utf-8 -*-
>> from flask import Flaskfrom flask import request, current_appfrom 
flask.ext.sqlalchemy import SQLAlchemyimport mymodules.users.controller as
MyMods
>>
>>
>> app = Flask(__name__)
>>
>> db = SQLAlchemy(app)
>> # [...]
>> @app.route(u"/test", methods=['GET'])def test():
>>
>>     return MyMods.mytest()
>> if __name__ == u"__main__":
>>     app.run()
>>
>> Content of controller.py
>>
>> # -*- coding: utf-8 -*-
>> def mytest():
>>     return "ok"
>>
>> Question: how can I use app and db inside controller.py?
>>
>> I can pass it as param:
>>
>> MyMods.mytest(app, db)
>> def mytest(app_ref, db_ref):
>>
>> but is it the right way (amongst Flask developers) of doing it?
>>
>>
>> --
>> Tomasz Sz.
>>
>>
>>
>>
>
>