librelist archives

« back to archive

About application init: object oriented way?

About application init: object oriented way?

From:
Kerem Ulutaş
Date:
2012-07-27 @ 12:14
Hi,

I am using Flask for a project at the moment. I am dealing with lots of CSS
/ Javascript issues at the moment. Yesterday, I looked at the entry point
of the application, and realized I wanted to try a more object oriented
approach, the entry point seems like this:


from flask import Flask, g, request, render_template
from jinja2 import FileSystemLoader
from os.path import join, dirname

ROOT_PATH = dirname(__file__)

app = Flask(__name__)
app.jinja_loader = FileSystemLoader(join(ROOT_PATH, "templates"))

@app.before_request
def before_request():
    g.img_host = "http://img.arabam.webfactional.com/"
    g.js_host = "http://js.arabam.webfactional.com/"
    g.style_host = "http://style.arabam.webfactional.com/"

@app.route('/', methods=['GET'])
def index():
    try:
        return render_template("index.html")
    except Exception as e:
        logger.exception(e)

if __name__ == '__main__':
    #app.run(host='0.0.0.0', port=5000, debug=True)
    app.run()


However, I want an object of a custom class to handle the app and other
stuff (like logging), I couldn't get it to work.


from flask import Flask, g, request, render_template
from jinja2 import FileSystemLoader
from os.path import join, dirname

ROOT_PATH = dirname(__file__)

class MyCustomClass(object):
    def __init__(self):
        self.app = Flask(__name__)
        self.app.jinja_loader = FileSystemLoader(join(ROOT_PATH,
"templates"))
        # some other useful stuff, like logging

    @app.before_request
    def before_request(self):
        g.img_host = "http://img.arabam.webfactional.com/"
        g.js_host = "http://js.arabam.webfactional.com/"
        g.style_host = "http://style.arabam.webfactional.com/"

    @app.route('/', methods=['GET'])
    def index(self):
        try:
            return render_template("index.html")
        except Exception as e:
            logger.exception(e)

if __name__ == '__main__':
    #app.run(host='0.0.0.0', port=5000, debug=True)
    app.run()


I realize that I can't use @self.app.route('/', methods=['GET']) - should I
derive from Flask object itself? Is this the only way (or would it even
work)? Please guide me.

Thanks,

Kerem Ulutas

-- 

Blog'umu okudunuz mu? http://www.ulutas.gen.tr

The box said "Requires Windows 95, NT, or better", so I installed Linux.

Re: [flask] About application init: object oriented way?

From:
kracekumar ramaraju
Date:
2012-07-28 @ 03:18
There is flas assests , mind giving a try.
 On Jul 27, 2012 5:46 PM, "Kerem Ulutaş" <1151986@gmail.com> wrote:

> Hi,
>
> I am using Flask for a project at the moment. I am dealing with lots of
> CSS / Javascript issues at the moment. Yesterday, I looked at the entry
> point of the application, and realized I wanted to try a more object
> oriented approach, the entry point seems like this:
>
>
> from flask import Flask, g, request, render_template
> from jinja2 import FileSystemLoader
> from os.path import join, dirname
>
> ROOT_PATH = dirname(__file__)
>
> app = Flask(__name__)
> app.jinja_loader = FileSystemLoader(join(ROOT_PATH, "templates"))
>
> @app.before_request
> def before_request():
>     g.img_host = "http://img.arabam.webfactional.com/"
>     g.js_host = "http://js.arabam.webfactional.com/"
>     g.style_host = "http://style.arabam.webfactional.com/"
>
> @app.route('/', methods=['GET'])
> def index():
>     try:
>         return render_template("index.html")
>     except Exception as e:
>         logger.exception(e)
>
> if __name__ == '__main__':
>     #app.run(host='0.0.0.0', port=5000, debug=True)
>     app.run()
>
>
> However, I want an object of a custom class to handle the app and other
> stuff (like logging), I couldn't get it to work.
>
>
> from flask import Flask, g, request, render_template
> from jinja2 import FileSystemLoader
> from os.path import join, dirname
>
> ROOT_PATH = dirname(__file__)
>
> class MyCustomClass(object):
>     def __init__(self):
>         self.app = Flask(__name__)
>         self.app.jinja_loader = FileSystemLoader(join(ROOT_PATH,
> "templates"))
>         # some other useful stuff, like logging
>
>     @app.before_request
>     def before_request(self):
>         g.img_host = "http://img.arabam.webfactional.com/"
>         g.js_host = "http://js.arabam.webfactional.com/"
>         g.style_host = "http://style.arabam.webfactional.com/"
>
>     @app.route('/', methods=['GET'])
>     def index(self):
>         try:
>             return render_template("index.html")
>         except Exception as e:
>             logger.exception(e)
>
> if __name__ == '__main__':
>     #app.run(host='0.0.0.0', port=5000, debug=True)
>     app.run()
>
>
> I realize that I can't use @self.app.route('/', methods=['GET']) - should
> I derive from Flask object itself? Is this the only way (or would it even
> work)? Please guide me.
>
> Thanks,
>
> Kerem Ulutas
>
> --
>
> Blog'umu okudunuz mu? http://www.ulutas.gen.tr
>
> The box said "Requires Windows 95, NT, or better", so I installed Linux.
>

Re: [flask] About application init: object oriented way?

From:
Kerem Ulutaş
Date:
2012-07-28 @ 03:32
Thanks for the suggestion, but my problem isn't about static sources - I
need something like Pluggable Views <http://flask.pocoo.org/docs/views/> but
for the app object itself.

I've been using CherryPy and I must say that Flask is a lot more polished &
decent looking library. However, I couldn't find the object oriented
approach for the app object (didn't try actually, I wanted to ask for you
guys' opinions)

2012/7/28 kracekumar ramaraju <kracethekingmaker@gmail.com>

> There is flas assests , mind giving a try.
>  On Jul 27, 2012 5:46 PM, "Kerem Ulutaş" <1151986@gmail.com> wrote:
>
>> Hi,
>>
>> I am using Flask for a project at the moment. I am dealing with lots of
>> CSS / Javascript issues at the moment. Yesterday, I looked at the entry
>> point of the application, and realized I wanted to try a more object
>> oriented approach, the entry point seems like this:
>>
>>
>> from flask import Flask, g, request, render_template
>> from jinja2 import FileSystemLoader
>> from os.path import join, dirname
>>
>> ROOT_PATH = dirname(__file__)
>>
>> app = Flask(__name__)
>> app.jinja_loader = FileSystemLoader(join(ROOT_PATH, "templates"))
>>
>> @app.before_request
>> def before_request():
>>     g.img_host = "http://img.arabam.webfactional.com/"
>>     g.js_host = "http://js.arabam.webfactional.com/"
>>     g.style_host = "http://style.arabam.webfactional.com/"
>>
>> @app.route('/', methods=['GET'])
>> def index():
>>     try:
>>         return render_template("index.html")
>>     except Exception as e:
>>         logger.exception(e)
>>
>> if __name__ == '__main__':
>>     #app.run(host='0.0.0.0', port=5000, debug=True)
>>     app.run()
>>
>>
>> However, I want an object of a custom class to handle the app and other
>> stuff (like logging), I couldn't get it to work.
>>
>>
>> from flask import Flask, g, request, render_template
>> from jinja2 import FileSystemLoader
>> from os.path import join, dirname
>>
>> ROOT_PATH = dirname(__file__)
>>
>> class MyCustomClass(object):
>>     def __init__(self):
>>         self.app = Flask(__name__)
>>         self.app.jinja_loader = FileSystemLoader(join(ROOT_PATH,
>> "templates"))
>>         # some other useful stuff, like logging
>>
>>     @app.before_request
>>     def before_request(self):
>>         g.img_host = "http://img.arabam.webfactional.com/"
>>         g.js_host = "http://js.arabam.webfactional.com/"
>>         g.style_host = "http://style.arabam.webfactional.com/"
>>
>>     @app.route('/', methods=['GET'])
>>     def index(self):
>>         try:
>>             return render_template("index.html")
>>         except Exception as e:
>>             logger.exception(e)
>>
>> if __name__ == '__main__':
>>     #app.run(host='0.0.0.0', port=5000, debug=True)
>>     app.run()
>>
>>
>> I realize that I can't use @self.app.route('/', methods=['GET']) - should
>> I derive from Flask object itself? Is this the only way (or would it even
>> work)? Please guide me.
>>
>> Thanks,
>>
>> Kerem Ulutas
>>
>> --
>>
>> Blog'umu okudunuz mu? http://www.ulutas.gen.tr
>>
>> The box said "Requires Windows 95, NT, or better", so I installed Linux.
>>
>


-- 

Blog'umu okudunuz mu? http://www.ulutas.gen.tr

The box said "Requires Windows 95, NT, or better", so I installed Linux.

Re: [flask] About application init: object oriented way?

From:
Steven Kryskalla
Date:
2012-07-28 @ 03:48
On Fri, Jul 27, 2012 at 8:32 PM, Kerem Ulutaş <1151986@gmail.com> wrote:
> Thanks for the suggestion, but my problem isn't about static sources - I
> need something like Pluggable Views but for the app object itself.
>

It sounds like you want an application factory:

http://flask.pocoo.org/docs/patterns/appfactories/

In your original code for the object oriented attempt, it won't work
because you're trying to decorate with "app", but app is defined as a
self attribute, so it does not exist within the body of the class.

I made an object oriented blueprint factory, with its own "route"
decorator which works just like app.route or blueprint.route. The code
is here:


https://bitbucket.org/lost_theory/flask-stripe-blueprint/src/513aae2dc2be/itemshop/__init__.py#cl-15

The same approach would work for the app object instead of a blueprint
object. Let me know if you want more explanation or code to show how.

-steve

Re: [flask] About application init: object oriented way?

From:
Steven Kryskalla
Date:
2012-07-28 @ 03:51
On Fri, Jul 27, 2012 at 8:48 PM, Steven Kryskalla <skryskalla@gmail.com> wrote:
> In your original code for the object oriented attempt, it won't work
> because you're trying to decorate with "app", but app is defined as a
> self attribute, so it does not exist within the body of the class.

Another thing you can do is avoid using the @app.route decorator, and
instead use self.app.add_url_rule inside a method to hook up all the
routes.