Saturday, 15 May 2010

python - Pass another object to the main flask application -



python - Pass another object to the main flask application -

i got uncertainty how pass object flask app in right way.

the thought simple.

i wan create api application, means http requests processed flask application trigger meothds in main application.

for that, need flask aware of other process, 1 way or another.

currently, have :

if __name__ == '__main__': logger = myprocess() app.run()

i need able :

@app.route('/nb_trendy') def nb_trendy(): res = logger.get_trendy() homecoming jsonify(res)

which means either need either give handle on logger app, or straight define logger within app.

but in each of solutions can find, need create inherits app, either override __init__() , takes objects parameters, or modify run() method.

the current way though :

class myflask(flask): def __init__(): flask.__init__() logger = myprocess()

is way it, or there improve way this?

thanks!

here more general version of question: web api in flask

take @ application factories, should you're looking for. you'd create mill returned flask app you'd send logger - this:

def create_app(logger_instance): app = flask(__name__) app.config['logger'] = logger_instance homecoming app

and in runserver.py, you'd create , pass in logger:

from yourapp import create_app if __name__ == '__main__': logger = myprocess() app = create_app(logger) app.run()

once that's done, app can refer logger within app.config['logger'].

python flask

No comments:

Post a Comment