from gevent.wsgi import WSGIServer from my_project.app import create_app from my_project.config import DevConfig app = create_app(DevConfig) http_server = WSGIServer(('', 5000), app) http_server.serve_forever()
def create_app(config=None, app_name=None, blueprints=None): app = Flask(app_name) configure_app(app, config) <other stuff> return app
@mymodule.route('/some/path/') def do_something(): do_stuff(something)
def async (f): def wrapper( * args, ** kwargs): t = Greenlet.spawn(f, * args, ** kwargs) gevent.joinall([t]) return wrapper
I have tried to add:
from myproject.app import create_app app = create_app() with app.app_context(): mail.send(msg)
For example, if you’ve got a command line app, you simply need to instantiate your app and push a context:
from flask import Flask, current_app app = Flask(__name__) with app.app_context(): # within this block, current_app points to app. print current_app.name
as blueprints can be registered multiple times with the application and not everything wants to be registered multiple times on it, this attribute can be used to figure out if the blueprint was registered in the past already.,Creates a copy of this request context with the same request object. This can be used to move a request context to a different greenlet. Because the actual request object is the same this cannot be used to move a request context to a different thread unless access to the request object is locked.,The request context contains all request relevant information. It is created at the beginning of the request and pushed to the _request_ctx_stack and removed at the end of it. It will create the URL adapter and request object for the WSGI environment provided.,Pops the request context and unbinds it by doing that. This will also trigger the execution of functions registered by the teardown_request() decorator.
from flask import _request_ctx_stack def get_session(): ctx = _request_ctx_stack.top if ctx is not None: return ctx.session
Flask solves this issue with the application context. Rather than referring to an app directly, you use the current_app proxy, which points to the application handling the current activity.,If you try to access current_app, or anything that uses it, outside an application context, you’ll get this error message:,If you see that error somewhere else in your code not related to configuring the application, it most likely indicates that you should move that code into a view function or CLI command.,Flask automatically pushes an application context when handling a request. View functions, error handlers, and other functions that run during a request will have access to current_app.
RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context().
def create_app(): app = Flask(__name__) with app.app_context(): init_db() return app
from flask import g def get_db(): if 'db' not in g: g.db = connect_to_database() return g.db @app.teardown_appcontext def teardown_db(exception): db = g.pop('db', None) if db is not None: db.close()
from werkzeug.local import LocalProxy db = LocalProxy(get_db)
Like Flask.teardown_request() but for a blueprint. Such a function is executed when tearing down each request, even if outside of the blueprint.,Like Flask.context_processor() but for a blueprint. Such a function is executed each request, even if outside of the blueprint.,Like Flask.after_request() but for a blueprint. Such a function is executed after each request, even if outside of the blueprint.,Like Flask.before_request(). Such a function is executed before each request, even if outside of a blueprint.
from flask import Flask app = Flask(__name__)
app = Flask('yourapplication') app = Flask(__name__.split('.'))
@app.route('/') def index(): pass
def index(): pass app.add_url_rule('/', 'index', index)
app.view_functions['index'] = index
with app.app_context(): ...