librelist archives

« back to archive

Flask: should I initiate one application per file or share same application?

Flask: should I initiate one application per file or share same application?

From:
sean chen
Date:
2013-07-13 @ 18:58
In this example,
https://github.com/thrisp/flask-celery-example/blob/master/app.py

I want to add sqlalchemy to build model, which is a separate model. As I
understand, to build DB session object, I need to initialize a Flask object
(https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/examples/hello.py
)


Should I put the Flask(__name__) application initialization into
__init__.py (in this way, the whole application only has a single Flask()
object)? Or should I have an Flask object in each module?

Basically, I guess I am asking how the Flask object is used across the web
app.

Thanks,
Sean

Re: [flask] Flask: should I initiate one application per file or share same application?

From:
Philippe Ndiaye
Date:
2013-07-13 @ 19:27
Hi!

The Flask object should be unique and declared in the __init__.py file,  of
your project (until you are working on a more complex project). So will be
your SQLAlchemy object.
Doing like this, you will be able to use it in your modules by importing
it. For example, assuming your project's name is foo your
foo.__init__.pyfile should look like that:

    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy

  application = Flask(__name__)
  db = SQLAlchemy(application)

Then, in your modules, you will just have to import the application object
from foo to set your routes by example, and your models will import the db
object to define your models, by example.


On Sat, Jul 13, 2013 at 8:58 PM, sean chen <alazylearner@gmail.com> wrote:

> In this example,
> https://github.com/thrisp/flask-celery-example/blob/master/app.py
>
> I want to add sqlalchemy to build model, which is a separate model. As I
> understand, to build DB session object, I need to initialize a Flask object
> (
> https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/examples/hello.py
> )
>
>
> Should I put the Flask(__name__) application initialization into
> __init__.py (in this way, the whole application only has a single Flask()
> object)? Or should I have an Flask object in each module?
>
> Basically, I guess I am asking how the Flask object is used across the web
> app.
>
> Thanks,
> Sean
>
>
>


-- 
Regards,
*
*
*Philippe Thécou NDIAYE*
IT Student @ SUPINFO Lyon
Linux Sysadmin  & Python programmer

Re: [flask] Flask: should I initiate one application per file or share same application?

From:
sean chen
Date:
2013-07-14 @ 05:07
Thanks a lot.
Here is my application layout:
myapp/
     __init__.py
     tasks.py  (define functions to process aynch tasks)
     view.py  (process httprequest, which will need to trigger an async
task)

If I want to have a Celery object to process a web request in background
asynchronously (and return a 202), should I have a tasks.py like this?

#__init__.py
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
  application = Flask(__name__)
  db = SQLAlchemy(application)
    celery = Celery(application)

#tasks.py
  from myapp import celery
  @celery.task(name="myapp.add")
  def add(x, y):
      return x + y



The above sounds reasonable to make a single Flask object. But to me, a
Celeryd is an independent separate process. Should I have a different Flask
in tasks.py? What is a Flask-friendly way?

    from flask import Flask
  application = Flask(__name__)
    celery = Celery(application)
    @celery.task(name="myapp.add")
    def add(x, y):
      return x + y

Thanks,
Sean


On Sat, Jul 13, 2013 at 12:27 PM, Philippe NDIAYE <phndiaye@gmail.com>wrote:

> Hi!
>
> The Flask object should be unique and declared in the __init__.py file,
>  of your project (until you are working on a more complex project). So will
> be your SQLAlchemy object.
> Doing like this, you will be able to use it in your modules by importing
> it. For example, assuming your project's name is foo your 
foo.__init__.pyfile should look like that:
>
>     from flask import Flask
>     from flask.ext.sqlalchemy import SQLAlchemy
>
>   application = Flask(__name__)
>   db = SQLAlchemy(application)
>
> Then, in your modules, you will just have to import the application object
> from foo to set your routes by example, and your models will import the
> db object to define your models, by example.
>
>
> On Sat, Jul 13, 2013 at 8:58 PM, sean chen <alazylearner@gmail.com> wrote:
>
>> In this example,
>> https://github.com/thrisp/flask-celery-example/blob/master/app.py
>>
>> I want to add sqlalchemy to build model, which is a separate model. As I
>> understand, to build DB session object, I need to initialize a Flask object
>> (
>> https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/examples/hello.py
>> )
>>
>>
>> Should I put the Flask(__name__) application initialization into
>> __init__.py (in this way, the whole application only has a single Flask()
>> object)? Or should I have an Flask object in each module?
>>
>> Basically, I guess I am asking how the Flask object is used across the
>> web app.
>>
>> Thanks,
>> Sean
>>
>>
>>
>
>
> --
> Regards,
> *
> *
> *Philippe Thécou NDIAYE*
> IT Student @ SUPINFO Lyon
> Linux Sysadmin  & Python programmer
>

Re: [flask] Flask: should I initiate one application per file or share same application?

From:
Philippe Ndiaye
Date:
2013-07-14 @ 06:44
According to the flask-celery example, you can use the same Flask
application than the rest of the application. But i never used Celery so my
knowledges are limited on it.

https://github.com/ask/flask-celery/blob/master/example/myapp.py


On Sun, Jul 14, 2013 at 7:07 AM, sean chen <alazylearner@gmail.com> wrote:

> Thanks a lot.
> Here is my application layout:
> myapp/
>      __init__.py
>      tasks.py  (define functions to process aynch tasks)
>      view.py  (process httprequest, which will need to trigger an async
> task)
>
> If I want to have a Celery object to process a web request in background
> asynchronously (and return a 202), should I have a tasks.py like this?
>
> #__init__.py
>     from flask import Flask
>     from flask.ext.sqlalchemy import SQLAlchemy
>   application = Flask(__name__)
>   db = SQLAlchemy(application)
>     celery = Celery(application)
>
> #tasks.py
>   from myapp import celery
>   @celery.task(name="myapp.add")
>   def add(x, y):
>       return x + y
>
>
>
> The above sounds reasonable to make a single Flask object. But to me, a
> Celeryd is an independent separate process. Should I have a different Flask
> in tasks.py? What is a Flask-friendly way?
>
>     from flask import Flask
>   application = Flask(__name__)
>     celery = Celery(application)
>     @celery.task(name="myapp.add")
>     def add(x, y):
>       return x + y
>
> Thanks,
> Sean
>
>
> On Sat, Jul 13, 2013 at 12:27 PM, Philippe NDIAYE <phndiaye@gmail.com>wrote:
>
>> Hi!
>>
>> The Flask object should be unique and declared in the __init__.py file,
>>  of your project (until you are working on a more complex project). So will
>> be your SQLAlchemy object.
>> Doing like this, you will be able to use it in your modules by importing
>> it. For example, assuming your project's name is foo your 
foo.__init__.pyfile should look like that:
>>
>>     from flask import Flask
>>     from flask.ext.sqlalchemy import SQLAlchemy
>>
>>   application = Flask(__name__)
>>   db = SQLAlchemy(application)
>>
>> Then, in your modules, you will just have to import the application
>> object from foo to set your routes by example, and your models will
>> import the db object to define your models, by example.
>>
>>
>> On Sat, Jul 13, 2013 at 8:58 PM, sean chen <alazylearner@gmail.com>wrote:
>>
>>> In this example,
>>> https://github.com/thrisp/flask-celery-example/blob/master/app.py
>>>
>>> I want to add sqlalchemy to build model, which is a separate model. As I
>>> understand, to build DB session object, I need to initialize a Flask object
>>> (
>>> https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/examples/hello.py
>>> )
>>>
>>>
>>> Should I put the Flask(__name__) application initialization into
>>> __init__.py (in this way, the whole application only has a single Flask()
>>> object)? Or should I have an Flask object in each module?
>>>
>>> Basically, I guess I am asking how the Flask object is used across the
>>> web app.
>>>
>>> Thanks,
>>> Sean
>>>
>>>
>>>
>>
>>
>> --
>> Regards,
>> *
>> *
>> *Philippe Thécou NDIAYE*
>> IT Student @ SUPINFO Lyon
>> Linux Sysadmin  & Python programmer
>>
>
>


-- 
Regards,
*
*
*Philippe Thécou NDIAYE*
IT Student @ SUPINFO Lyon
Linux Sysadmin  & Python programmer