librelist archives

« back to archive

Good Skeleton for a Flask application

Good Skeleton for a Flask application

From:
Alberto Piu
Date:
2011-08-15 @ 09:03
Hi there,

I read about this in this mailing list, but I think I need further explanations.
I'm currently writing a web app with Flask, and considering the size of 
the project I decided to divide the code logically in modules. When doing 
this, though, I am actually unable to import modules from the main "app" 
.py while importing the "app" .py from within the modules. Here is an 
example:

	# main.py

	from flask import Flask
	app = Flask(__name__) 

	app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'

	import database

	# IMPORTANT PART HERE
	# Here I would like to use the flashyObject
	# created in database.py, but I cant.
	.
	.. routes and .run() ...

	

	# database.py

	import main
	
	# HERE I need to import main because I need the 
	# app object. But this causes flashy
	# object to be unavailable in main.py

	db = SQLAlchemy(app)

	... flashy statements ...

	flashyObject = somethingCreatedFromThe(db)

I know the solution may be trivial, but I can't get it working. I know I 
can have one big main.py with everything inside of it, but there has to be
a better solution.

Thank you in advance!
Alberto
	

Re: [flask] Good Skeleton for a Flask application

From:
Jackie Lee
Date:
2011-08-15 @ 09:35
Hi

I think you can use: from flask import current_app

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


http://flask.pocoo.org/mailinglist/archive/2011/7/14/accessing-the-application-object-from-a-blueprint/#e49d2ddf168d21d147cdebe8ddaef692

On Mon, Aug 15, 2011 at 10:03 AM, Alberto Piu <piu.alberto@gmail.com> wrote:
> Hi there,
>
> I read about this in this mailing list, but I think I need further explanations.
> I'm currently writing a web app with Flask, and considering the size of 
the project I decided to divide the code logically in modules. When doing 
this, though, I am actually unable to import modules from the main "app" 
.py while importing the "app" .py from within the modules. Here is an 
example:
>
>        # main.py
>
>        from flask import Flask
>        app = Flask(__name__)
>
>        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
>
>        import database
>
>        # IMPORTANT PART HERE
>        # Here I would like to use the flashyObject
>        # created in database.py, but I cant.
>        .
>        .. routes and .run() ...
>
>
>
>        # database.py
>
>        import main
>
>        # HERE I need to import main because I need the
>        # app object. But this causes flashy
>        # object to be unavailable in main.py
>
>        db = SQLAlchemy(app)
>
>        ... flashy statements ...
>
>        flashyObject = somethingCreatedFromThe(db)
>
> I know the solution may be trivial, but I can't get it working. I know I
can have one big main.py with everything inside of it, but there has to be
a better solution.
>
> Thank you in advance!
> Alberto
>
>



-- 
Jackie

Re: [flask] Good Skeleton for a Flask application

From:
Alberto Piu
Date:
2011-08-15 @ 13:30
Hi there,

I'm sorry for writing again. I see that 

> current_app only works when a request is active. 
> That's a side effect or how Python works internally which I don't want 
to hack around.

	(mitsuhiko)

So I think this is not going to help me. I get the error:

	RuntimeError: working outside of request context

This is driving me crazy. It is not possible that I have to group 
everything in one file :/
So I think I am misunderstanding the whole thing, and that there is a wrong way 
to split things and a right one.

Any ideas?
Thank you.

Alberto


Il giorno 15/ago/2011, alle ore 11:39, Alberto Piu ha scritto:

> Hi Jackie, that was quick! Thank you, this is very interesting and useful.
> 
> Thank you a lot!
> Alberto
> 
> 
> Il giorno 15/ago/2011, alle ore 11:35, Jackie Lee ha scritto:
> 
>> Hi
>> 
>> I think you can use: from flask import current_app
>> 
>> http://flask.pocoo.org/docs/api/#flask.current_app
>> 
>> 
http://flask.pocoo.org/mailinglist/archive/2011/7/14/accessing-the-application-object-from-a-blueprint/#e49d2ddf168d21d147cdebe8ddaef692
>> 
>> On Mon, Aug 15, 2011 at 10:03 AM, Alberto Piu <piu.alberto@gmail.com> wrote:
>>> Hi there,
>>> 
>>> I read about this in this mailing list, but I think I need further 
explanations.
>>> I'm currently writing a web app with Flask, and considering the size 
of the project I decided to divide the code logically in modules. When 
doing this, though, I am actually unable to import modules from the main 
"app" .py while importing the "app" .py from within the modules. Here is 
an example:
>>> 
>>>      # main.py
>>> 
>>>      from flask import Flask
>>>      app = Flask(__name__)
>>> 
>>>      app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
>>> 
>>>      import database
>>> 
>>>      # IMPORTANT PART HERE
>>>      # Here I would like to use the flashyObject
>>>      # created in database.py, but I cant.
>>>      .
>>>      .. routes and .run() ...
>>> 
>>> 
>>> 
>>>      # database.py
>>> 
>>>      import main
>>> 
>>>      # HERE I need to import main because I need the
>>>      # app object. But this causes flashy
>>>      # object to be unavailable in main.py
>>> 
>>>      db = SQLAlchemy(app)
>>> 
>>>      ... flashy statements ...
>>> 
>>>      flashyObject = somethingCreatedFromThe(db)
>>> 
>>> I know the solution may be trivial, but I can't get it working. I know
I can have one big main.py with everything inside of it, but there has to 
be a better solution.
>>> 
>>> Thank you in advance!
>>> Alberto
>>> 
>>> 
>> 
>> 
>> 
>> -- 
>> Jackie
> 

Re: [flask] Good Skeleton for a Flask application

From:
Daniel , Dao Quang Minh
Date:
2011-08-15 @ 13:50
I would use `init_app` instead of passing the app with Flask-SQLAlchemy
constructor. For example:

# database
db = SQLAlchemy()

# main
app = Flask(app)
from database import db
db.init_app(app)

# use db for flashy_object


Hope that i understood your requirements correctly.

Daniel

On Mon, Aug 15, 2011 at 9:30 PM, Alberto Piu <piu.alberto@gmail.com> wrote:

>
> Hi there,
>
> I'm sorry for writing again. I see that
>
> > current_app only works when a request is active.
> > That's a side effect or how Python works internally which I don't want to
> hack around.
>
>        (mitsuhiko)
>
> So I think this is not going to help me. I get the error:
>
>        RuntimeError: working outside of request context
>
> This is driving me crazy. It is not possible that I have to group
> everything in one file :/
> So I think I am misunderstanding the whole thing, and that there is a wrong
> way
> to split things and a right one.
>
> Any ideas?
> Thank you.
>
> Alberto
>
>
> Il giorno 15/ago/2011, alle ore 11:39, Alberto Piu ha scritto:
>
> > Hi Jackie, that was quick! Thank you, this is very interesting and
> useful.
> >
> > Thank you a lot!
> > Alberto
> >
> >
> > Il giorno 15/ago/2011, alle ore 11:35, Jackie Lee ha scritto:
> >
> >> Hi
> >>
> >> I think you can use: from flask import current_app
> >>
> >> http://flask.pocoo.org/docs/api/#flask.current_app
> >>
> >>
> 
http://flask.pocoo.org/mailinglist/archive/2011/7/14/accessing-the-application-object-from-a-blueprint/#e49d2ddf168d21d147cdebe8ddaef692
> >>
> >> On Mon, Aug 15, 2011 at 10:03 AM, Alberto Piu <piu.alberto@gmail.com>
> wrote:
> >>> Hi there,
> >>>
> >>> I read about this in this mailing list, but I think I need further
> explanations.
> >>> I'm currently writing a web app with Flask, and considering the size of
> the project I decided to divide the code logically in modules. When doing
> this, though, I am actually unable to import modules from the main "app" .py
> while importing the "app" .py from within the modules. Here is an example:
> >>>
> >>>      # main.py
> >>>
> >>>      from flask import Flask
> >>>      app = Flask(__name__)
> >>>
> >>>      app.config['SQLALCHEMY_DATABASE_URI'] =
> 'sqlite:///database.sqlite'
> >>>
> >>>      import database
> >>>
> >>>      # IMPORTANT PART HERE
> >>>      # Here I would like to use the flashyObject
> >>>      # created in database.py, but I cant.
> >>>      .
> >>>      .. routes and .run() ...
> >>>
> >>>
> >>>
> >>>      # database.py
> >>>
> >>>      import main
> >>>
> >>>      # HERE I need to import main because I need the
> >>>      # app object. But this causes flashy
> >>>      # object to be unavailable in main.py
> >>>
> >>>      db = SQLAlchemy(app)
> >>>
> >>>      ... flashy statements ...
> >>>
> >>>      flashyObject = somethingCreatedFromThe(db)
> >>>
> >>> I know the solution may be trivial, but I can't get it working. I know
> I can have one big main.py with everything inside of it, but there has to be
> a better solution.
> >>>
> >>> Thank you in advance!
> >>> Alberto
> >>>
> >>>
> >>
> >>
> >>
> >> --
> >> Jackie
> >
>
>
>

Re: [flask] Good Skeleton for a Flask application

From:
Alberto Piu
Date:
2011-08-15 @ 14:08
Yes, you understood perfectly.

As expected I was doing it wrong--thank you, this seems to work now.

Alberto 

Il giorno 15/ago/2011, alle ore 15:50, Daniel , Dao Quang Minh ha scritto:

> I would use `init_app` instead of passing the app with Flask-SQLAlchemy 
constructor. For example:
> 
> # database
> db = SQLAlchemy()
> 
> # main
> app = Flask(app)
> from database import db
> db.init_app(app)
> 
> # use db for flashy_object
> 
> Hope that i understood your requirements correctly.
> 
> Daniel
> 
> On Mon, Aug 15, 2011 at 9:30 PM, Alberto Piu <piu.alberto@gmail.com> wrote:
> 
> Hi there,
> 
> I'm sorry for writing again. I see that
> 
> > current_app only works when a request is active.
> > That's a side effect or how Python works internally which I don't want
to hack around.
> 
>        (mitsuhiko)
> 
> So I think this is not going to help me. I get the error:
> 
>        RuntimeError: working outside of request context
> 
> This is driving me crazy. It is not possible that I have to group 
everything in one file :/
> So I think I am misunderstanding the whole thing, and that there is a wrong way
> to split things and a right one.
> 
> Any ideas?
> Thank you.
> 
> Alberto
> 
> 
> Il giorno 15/ago/2011, alle ore 11:39, Alberto Piu ha scritto:
> 
> > Hi Jackie, that was quick! Thank you, this is very interesting and useful.
> >
> > Thank you a lot!
> > Alberto
> >
> >
> > Il giorno 15/ago/2011, alle ore 11:35, Jackie Lee ha scritto:
> >
> >> Hi
> >>
> >> I think you can use: from flask import current_app
> >>
> >> http://flask.pocoo.org/docs/api/#flask.current_app
> >>
> >> 
http://flask.pocoo.org/mailinglist/archive/2011/7/14/accessing-the-application-object-from-a-blueprint/#e49d2ddf168d21d147cdebe8ddaef692
> >>
> >> On Mon, Aug 15, 2011 at 10:03 AM, Alberto Piu <piu.alberto@gmail.com> wrote:
> >>> Hi there,
> >>>
> >>> I read about this in this mailing list, but I think I need further 
explanations.
> >>> I'm currently writing a web app with Flask, and considering the size
of the project I decided to divide the code logically in modules. When 
doing this, though, I am actually unable to import modules from the main 
"app" .py while importing the "app" .py from within the modules. Here is 
an example:
> >>>
> >>>      # main.py
> >>>
> >>>      from flask import Flask
> >>>      app = Flask(__name__)
> >>>
> >>>      app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
> >>>
> >>>      import database
> >>>
> >>>      # IMPORTANT PART HERE
> >>>      # Here I would like to use the flashyObject
> >>>      # created in database.py, but I cant.
> >>>      .
> >>>      .. routes and .run() ...
> >>>
> >>>
> >>>
> >>>      # database.py
> >>>
> >>>      import main
> >>>
> >>>      # HERE I need to import main because I need the
> >>>      # app object. But this causes flashy
> >>>      # object to be unavailable in main.py
> >>>
> >>>      db = SQLAlchemy(app)
> >>>
> >>>      ... flashy statements ...
> >>>
> >>>      flashyObject = somethingCreatedFromThe(db)
> >>>
> >>> I know the solution may be trivial, but I can't get it working. I 
know I can have one big main.py with everything inside of it, but there 
has to be a better solution.
> >>>
> >>> Thank you in advance!
> >>> Alberto
> >>>
> >>>
> >>
> >>
> >>
> >> --
> >> Jackie
> >
> 
> 
> 

Re: [flask] Good Skeleton for a Flask application

From:
Alberto Piu
Date:
2011-08-15 @ 09:39
Hi Jackie, that was quick! Thank you, this is very interesting and useful.

Thank you a lot!
Alberto


Il giorno 15/ago/2011, alle ore 11:35, Jackie Lee ha scritto:

> Hi
> 
> I think you can use: from flask import current_app
> 
> http://flask.pocoo.org/docs/api/#flask.current_app
> 
> 
http://flask.pocoo.org/mailinglist/archive/2011/7/14/accessing-the-application-object-from-a-blueprint/#e49d2ddf168d21d147cdebe8ddaef692
> 
> On Mon, Aug 15, 2011 at 10:03 AM, Alberto Piu <piu.alberto@gmail.com> wrote:
>> Hi there,
>> 
>> I read about this in this mailing list, but I think I need further 
explanations.
>> I'm currently writing a web app with Flask, and considering the size of
the project I decided to divide the code logically in modules. When doing 
this, though, I am actually unable to import modules from the main "app" 
.py while importing the "app" .py from within the modules. Here is an 
example:
>> 
>>        # main.py
>> 
>>        from flask import Flask
>>        app = Flask(__name__)
>> 
>>        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
>> 
>>        import database
>> 
>>        # IMPORTANT PART HERE
>>        # Here I would like to use the flashyObject
>>        # created in database.py, but I cant.
>>        .
>>        .. routes and .run() ...
>> 
>> 
>> 
>>        # database.py
>> 
>>        import main
>> 
>>        # HERE I need to import main because I need the
>>        # app object. But this causes flashy
>>        # object to be unavailable in main.py
>> 
>>        db = SQLAlchemy(app)
>> 
>>        ... flashy statements ...
>> 
>>        flashyObject = somethingCreatedFromThe(db)
>> 
>> I know the solution may be trivial, but I can't get it working. I know 
I can have one big main.py with everything inside of it, but there has to 
be a better solution.
>> 
>> Thank you in advance!
>> Alberto
>> 
>> 
> 
> 
> 
> -- 
> Jackie