librelist archives

« back to archive

Pass another object to the main flask application

Pass another object to the main flask application

From:
julien lengrand-lambert
Date:
2013-02-14 @ 11:59
Hi all,


I got troubles about how to pass an object to a flask app in the correct
way.

The idea is simple.

I wan to create an api for my application, which means that http requests
processed by my flask application will trigger meothds in my main
application.

For that, I need flask to be aware of my other process, one way or another.

Currently, I have something like :

if __name__ == '__main__':
    Logger = MyProcess()
    app.run()

 I need to be able to do something like that :

@app.route('/nb_trendy')def nb_trendy():

    res = Logger.get_trendy()
    return jsonify(res)

 which means that I either need to either give a handle on Logger to app,
or directly define Logger inside app.

But in each of the solutions I can find, I need to create a that
*inherits* app,
to either override *_init_()*and takes objects as parameters, or modify the
*run()* method.

the current way I would do it is though :

class MyFlask(Flask):
    def __init__():
        Flask.__init__()
        Logger = MyProcess()

 Is this also the way you would do it, or is there a better way to do this ?

Thanks!


Julien

Re: [flask] Pass another object to the main flask application

From:
Trey Long
Date:
2013-02-14 @ 14:12
From your example it doesn't look like Logger is dependent on the API 
flask application.

Why not initialize Logger in the module and then attach it to the "g" 
object using the "before_request" decorator?


logger = create_logger_app()

@before_request
def init():
	g.logger = logger


Your example might be too basic for me to understand the problem.


On Feb 14, 2013, at 6:59 AM, julien lengrand-lambert <jlengrand@gmail.com> wrote:

> Hi all, 
> 
> 
> I got troubles about how to pass an object to a flask app in the correct way.
> 
> The idea is simple.
> 
> I wan to create an api for my application, which means that http 
requests processed by my flask application will trigger meothds in my main
application.
> 
> For that, I need flask to be aware of my other process, one way or another.
> 
> Currently, I have something like :
> 
> 
> if __name__ == '__main__':
>     Logger = MyProcess()
>     app.run()
> I need to be able to do something like that :
> 
> 
> @app.route('/nb_trendy')
> def nb_trendy():
> 
>     res = Logger.get_trendy()
>     return jsonify(res)
> which means that I either need to either give a handle on Logger to app,
or directly define Logger inside app.
> 
> But in each of the solutions I can find, I need to create a that 
inherits app, to either override _init_()and takes objects as parameters, 
or modify the run() method.
> 
> the current way I would do it is though :
> 
> 
> class MyFlask(Flask):
>     def __init__():
>         Flask.__init__()
>         Logger = MyProcess()
> Is this also the way you would do it, or is there a better way to do this ?
> 
> Thanks!
> 
> 
> 
> Julien
> 

Re: [flask] Pass another object to the main flask application

From:
julien lengrand-lambert
Date:
2013-02-14 @ 15:34
It might actually be what I am searching for !

My concern is that I want the same instance of Logger to be used all the
time, and not create a new instance of Logger for each incoming requests.
Logger is the part that contains all the databases connections and I need
to have only 1 Logger running at all time.

Thx for answering!


On Thu, Feb 14, 2013 at 3:12 PM, Trey Long <trey@ktrl.com> wrote:

> From your example it doesn't look like Logger is dependent on the API
> flask application.
>
> Why not initialize Logger in the module and then attach it to the "g"
> object using the "before_request" decorator?
>
>
> logger = create_logger_app()
>
> @before_request
> def init():
> g.logger = logger
>
>
> Your example might be too basic for me to understand the problem.
>
>
> On Feb 14, 2013, at 6:59 AM, julien lengrand-lambert <jlengrand@gmail.com>
> wrote:
>
> Hi all,
>
>
> I got troubles about how to pass an object to a flask app in the correct
> way.
>
> The idea is simple.
>
> I wan to create an api for my application, which means that http requests
> processed by my flask application will trigger meothds in my main
> application.
>
> For that, I need flask to be aware of my other process, one way or another.
>
> Currently, I have something like :
>
> if __name__ == '__main__':
>     Logger = MyProcess()
>     app.run()
>
>  I need to be able to do something like that :
>
> @app.route('/nb_trendy')def nb_trendy():
>
>     res = Logger.get_trendy()
>     return jsonify(res)
>
>  which means that I either need to either give a handle on Logger to app,
> or directly define Logger inside app.
>
> But in each of the solutions I can find, I need to create a that *inherits
> * app, to either override *_init_()*and takes objects as parameters, or
> modify the *run()* method.
>
> the current way I would do it is though :
>
> class MyFlask(Flask):
>     def __init__():
>         Flask.__init__()
>         Logger = MyProcess()
>
>  Is this also the way you would do it, or is there a better way to do this
> ?
>
> Thanks!
>
>
> Julien
>
>
>

Re: [flask] Pass another object to the main flask application

From:
Trey Long
Date:
2013-02-14 @ 16:31
Well, if you initialize it at the module level, per normal Python rules 
that would give you what you're looking for. Assuming there are no other 
weird dependencies involved.

On Feb 14, 2013, at 10:34 AM, julien lengrand-lambert <jlengrand@gmail.com> wrote:

> It might actually be what I am searching for !
> 
> My concern is that I want the same instance of Logger to be used all the
time, and not create a new instance of Logger for each incoming requests.
> Logger is the part that contains all the databases connections and I 
need to have only 1 Logger running at all time. 
> 
> Thx for answering!
> 
> 
> On Thu, Feb 14, 2013 at 3:12 PM, Trey Long <trey@ktrl.com> wrote:
> From your example it doesn't look like Logger is dependent on the API 
flask application.
> 
> Why not initialize Logger in the module and then attach it to the "g" 
object using the "before_request" decorator?
> 
> 
> logger = create_logger_app()
> 
> @before_request
> def init():
> 	g.logger = logger
> 
> 
> Your example might be too basic for me to understand the problem.
> 
> 
> On Feb 14, 2013, at 6:59 AM, julien lengrand-lambert 
<jlengrand@gmail.com> wrote:
> 
> Hi all, 
> 
> 
> I got troubles about how to pass an object to a flask app in the correct way.
> 
> The idea is simple.
> 
> I wan to create an api for my application, which means that http 
requests processed by my flask application will trigger meothds in my main
application.
> 
> For that, I need flask to be aware of my other process, one way or another.
> 
> Currently, I have something like :
> 
> 
> if __name__ == '__main__':
>     Logger = MyProcess()
>     app.run()
> I need to be able to do something like that :
> 
> 
> @app.route('/nb_trendy')
> def nb_trendy():
> 
>     res = Logger.get_trendy()
>     return jsonify(res)
> which means that I either need to either give a handle on Logger to app,
or directly define Logger inside app.
> 
> But in each of the solutions I can find, I need to create a that 
inherits app, to either override _init_()and takes objects as parameters, 
or modify the run() method.
> 
> the current way I would do it is though :
> 
> 
> class MyFlask(Flask):
>     def __init__():
>         Flask.__init__()
>         Logger = MyProcess()
> Is this also the way you would do it, or is there a better way to do this ?
> 
> Thanks!
> 
> 
> 
> Julien
> 
> 
> 

Re: [flask] Pass another object to the main flask application

From:
julien lengrand-lambert
Date:
2013-02-14 @ 23:56
Hey,

I tried what you told me, but it is actually causing me problems because if
I do that I lose the handle over logger.

My concern is actually that I do not know how to use flask correctly so
that it can use an object that has been defined somewhere else.

In my case, Logger is a process. I need to provide ways to interact with it
(start, stop, terminate, ...).
Flask in this case is a way for me to create some kind of web api around it
so that I can trigger methods around it.


You can look here (
https://github.com/jlengrand/twitterboard/blob/master/twiderboard/command.py)
how I currently use the Logger.
This is a command line that is used to add/remove hashtags to be tracked..

The actual interesting part is the parse method :

def parse(self, comm):
        """
        Parses the command input by the user
        and triggers the corresponding action
        """

        word = comm.lower()
        if word in ["h", "help", "?"]:
            self.help()
        else:
            if word.startswith("add #"):
                hashtag = word.replace("add ", "")
                self.h.add_hashtag(hashtag)
            elif word.startswith("rm #"):
                hashtag = word.replace("rm ", "")
                self.h.remove_hashtag(hashtag)
            else:
                print "Unrecognized command"


where, I trigger remove_hashtag and ad_hashtag based on user input.
I want to be able to use those in a object oriented context.
If I just define them in the module, I lose all flexibility to use them
later on.


What I would like in the end is just a way to be able to interact with my
API defined as a class also using HTTP REquests.

Is it a bit clearer?

Thanks a lot,
Julien



On Thu, Feb 14, 2013 at 5:31 PM, Trey Long <trey@ktrl.com> wrote:

> Well, if you initialize it at the module level, per normal Python rules
> that would give you what you're looking for. Assuming there are no other
> weird dependencies involved.
>
> On Feb 14, 2013, at 10:34 AM, julien lengrand-lambert <jlengrand@gmail.com>
> wrote:
>
> It might actually be what I am searching for !
>
> My concern is that I want the same instance of Logger to be used all the
> time, and not create a new instance of Logger for each incoming requests.
> Logger is the part that contains all the databases connections and I need
> to have only 1 Logger running at all time.
>
> Thx for answering!
>
>
> On Thu, Feb 14, 2013 at 3:12 PM, Trey Long <trey@ktrl.com> wrote:
>
>> From your example it doesn't look like Logger is dependent on the API
>> flask application.
>>
>> Why not initialize Logger in the module and then attach it to the "g"
>> object using the "before_request" decorator?
>>
>>
>> logger = create_logger_app()
>>
>> @before_request
>> def init():
>> g.logger = logger
>>
>>
>> Your example might be too basic for me to understand the problem.
>>
>>
>> On Feb 14, 2013, at 6:59 AM, julien lengrand-lambert <jlengrand@gmail.com>
>> wrote:
>>
>> Hi all,
>>
>>
>> I got troubles about how to pass an object to a flask app in the correct
>> way.
>>
>> The idea is simple.
>>
>> I wan to create an api for my application, which means that http requests
>> processed by my flask application will trigger meothds in my main
>> application.
>>
>> For that, I need flask to be aware of my other process, one way or
>> another.
>>
>> Currently, I have something like :
>>
>> if __name__ == '__main__':
>>     Logger = MyProcess()
>>     app.run()
>>
>>  I need to be able to do something like that :
>>
>> @app.route('/nb_trendy')def nb_trendy():
>>
>>     res = Logger.get_trendy()
>>     return jsonify(res)
>>
>>  which means that I either need to either give a handle on Logger to app,
>> or directly define Logger inside app.
>>
>> But in each of the solutions I can find, I need to create a that *
>> inherits* app, to either override *_init_()*and takes objects as
>> parameters, or modify the *run()* method.
>>
>> the current way I would do it is though :
>>
>> class MyFlask(Flask):
>>     def __init__():
>>         Flask.__init__()
>>         Logger = MyProcess()
>>
>>  Is this also the way you would do it, or is there a better way to do
>> this ?
>>
>> Thanks!
>>
>>
>> Julien
>>
>>
>>
>
>

Re: [flask] Pass another object to the main flask application

From:
Trey Long
Date:
2013-02-15 @ 01:00
I took a look at command.py and noticed that you are waiting for an input 
loop, this cannot exist in the same thread/process as your Flask server 
because it blocks indefinitely.

You must use Python's threading modules to begin threads or processes and 
communicate between them using standard threadsafe data structures or IPC 
channels.


On Feb 14, 2013, at 6:56 PM, julien lengrand-lambert <jlengrand@gmail.com> wrote:

> Hey, 
> 
> I tried what you told me, but it is actually causing me problems because
if I do that I lose the handle over logger. 
> 
> My concern is actually that I do not know how to use flask correctly so 
that it can use an object that has been defined somewhere else.
> 
> In my case, Logger is a process. I need to provide ways to interact with
it (start, stop, terminate, ...).
> Flask in this case is a way for me to create some kind of web api around
it so that I can trigger methods around it. 
> 
> 
> You can look here 
(https://github.com/jlengrand/twitterboard/blob/master/twiderboard/command.py)
how I currently use the Logger.
> This is a command line that is used to add/remove hashtags to be tracked..
> 
> The actual interesting part is the parse method :
> 
> 
> def parse(self, comm):
> 
> 
>         """
> 
> 
>         Parses the command input by the user
>         and triggers the corresponding action
> 
> 
>         """
> 
> 
>         word = comm.lower()
> 
> 
>         if word in ["h", "help", "?"]:
> 
> 
>             self.help()
> 
> 
>         else:
> 
> 
>             if word.startswith("add #"):
> 
> 
>                 hashtag = word.replace("add ", "")
> 
> 
>                 self.h.add_hashtag(hashtag)
> 
> 
>             elif word.startswith("rm #"):
> 
> 
>                 hashtag = word.replace("rm ", "")
> 
> 
>                 self.h.remove_hashtag(hashtag)
> 
> 
>             else:
> 
> 
>                 print "Unrecognized command"
> 
> 
> 
> where, I trigger remove_hashtag and ad_hashtag based on user input.
> I want to be able to use those in a object oriented context. 
> If I just define them in the module, I lose all flexibility to use them 
later on.
> 
> 
> What I would like in the end is just a way to be able to interact with 
my API defined as a class also using HTTP REquests. 
> 
> Is it a bit clearer? 
> 
> Thanks a lot, 
> Julien
> 
> 
> 
> On Thu, Feb 14, 2013 at 5:31 PM, Trey Long <trey@ktrl.com> wrote:
> Well, if you initialize it at the module level, per normal Python rules 
that would give you what you're looking for. Assuming there are no other 
weird dependencies involved.
> 
> On Feb 14, 2013, at 10:34 AM, julien lengrand-lambert 
<jlengrand@gmail.com> wrote:
> 
>> It might actually be what I am searching for !
>> 
>> My concern is that I want the same instance of Logger to be used all 
the time, and not create a new instance of Logger for each incoming 
requests.
>> Logger is the part that contains all the databases connections and I 
need to have only 1 Logger running at all time. 
>> 
>> Thx for answering!
>> 
>> 
>> On Thu, Feb 14, 2013 at 3:12 PM, Trey Long <trey@ktrl.com> wrote:
>> From your example it doesn't look like Logger is dependent on the API 
flask application.
>> 
>> Why not initialize Logger in the module and then attach it to the "g" 
object using the "before_request" decorator?
>> 
>> 
>> logger = create_logger_app()
>> 
>> @before_request
>> def init():
>> 	g.logger = logger
>> 
>> 
>> Your example might be too basic for me to understand the problem.
>> 
>> 
>> On Feb 14, 2013, at 6:59 AM, julien lengrand-lambert 
<jlengrand@gmail.com> wrote:
>> 
>> Hi all, 
>> 
>> 
>> I got troubles about how to pass an object to a flask app in the correct way.
>> 
>> The idea is simple.
>> 
>> I wan to create an api for my application, which means that http 
requests processed by my flask application will trigger meothds in my main
application.
>> 
>> For that, I need flask to be aware of my other process, one way or another.
>> 
>> Currently, I have something like :
>> 
>> 
>> if __name__ == '__main__':
>>     Logger = MyProcess()
>>     app.run()
>> I need to be able to do something like that :
>> 
>> 
>> @app.route('/nb_trendy')
>> def nb_trendy():
>> 
>>     res = Logger.get_trendy()
>>     return jsonify(res)
>> which means that I either need to either give a handle on Logger to 
app, or directly define Logger inside app.
>> 
>> But in each of the solutions I can find, I need to create a that 
inherits app, to either override _init_()and takes objects as parameters, 
or modify the run() method.
>> 
>> the current way I would do it is though :
>> 
>> 
>> class MyFlask(Flask):
>>     def __init__():
>>         Flask.__init__()
>>         Logger = MyProcess()
>> Is this also the way you would do it, or is there a better way to do this ?
>> 
>> Thanks!
>> 
>> 
>> 
>> Julien
>> 
>> 
>> 
> 
>