<Flask> outside of application context

Flaskさんのオブジェクトで少し遊ぶ時、時々遭遇する、outside of application context
withapp.app_context()を呼べばいいことがわかった。

The Application Context — Flask Documentation (0.10)

例。

まずは、Flaskインスタンスapp作る。

In [60]: from flask import Flask, current_app

In [61]: app = Flask(__name__)

で、アクセス。

In [62]: current_app.name
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-62-c38297bb9c58> in <module>()
----> 1 current_app.name

C:\Anaconda3\Lib\site-packages\werkzeug\local.py in __getattr__(self, name)
    341         if name == '__members__':
    342             return dir(self._get_current_object())
--> 343         return getattr(self._get_current_object(), name)
    344 
    345     def __setitem__(self, key, value):

C:\Anaconda3\Lib\site-packages\werkzeug\local.py in _get_current_object(self)
    300         """
    301         if not hasattr(self.__local, '__release_local__'):
--> 302             return self.__local()
    303         try:
    304             return getattr(self.__local, self.__name__)

C:\Anaconda3\Lib\site-packages\flask\globals.py in _find_app()
     32     top = _app_ctx_stack.top
     33     if top is None:
---> 34         raise RuntimeError('working outside of application context')
     35     return top.app
     36 

RuntimeError: working outside of application context

ゲロ。
で、withする。

In [63]: with app.app_context():
    ...:     print(current_app.name)
    ...:     
__main__

オッケー。

なるほどね。

ちなみに、、、
current_appアトリビュートは、

In [64]: dir(current_app)
Out[64]: []

コンテキスト外だと空。
で、中だと、

In [65]: with app.app_context():
    ...:     print(dir(current_app))
    ...:     
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_before_request_lock', '_error_handlers', '_got_first_request', '_logger', '_register_error_handler', '_static_folder', '_static_url_path', 'add_template_filter', 'add_template_global', 'add_template_test', 'add_url_rule', 'after_request', 'after_request_funcs', 'app_context', 'app_ctx_globals_class', 'auto_find_instance_path', 'before_first_request', 'before_first_request_funcs', 'before_request', 'before_request_funcs', 'blueprints', 'config', 'context_processor', 'create_global_jinja_loader', 'create_jinja_environment', 'create_url_adapter', 'debug', 'debug_log_format', 'default_config', 'dispatch_request', 'do_teardown_appcontext', 'do_teardown_request', 'enable_modules', 'endpoint', 'error_handler_spec', 'error_handlers', 'errorhandler', 'extensions', 'full_dispatch_request', 'get_send_file_max_age', 'got_first_request', 'handle_exception', 'handle_http_exception', 'handle_url_build_error', 'handle_user_exception', 'has_static_folder', 'import_name', 'init_jinja_globals', 'inject_url_defaults', 'instance_path', 'jinja_env', 'jinja_loader', 'jinja_options', 'json_decoder', 'json_encoder', 'log_exception', 'logger', 'logger_name', 'make_config', 'make_default_options_response', 'make_null_session', 'make_response', 'modules', 'name', 'open_instance_resource', 'open_resource', 'open_session', 'permanent_session_lifetime', 'preprocess_request', 'preserve_context_on_exception', 'process_response', 'propagate_exceptions', 'raise_routing_exception', 'register_blueprint', 'register_error_handler', 'register_module', 'request_class', 'request_context', 'request_globals_class', 'response_class', 'root_path', 'route', 'run', 'save_session', 'secret_key', 'select_jinja_autoescape', 'send_static_file', 'session_cookie_name', 'session_interface', 'should_ignore_error', 'static_folder', 'static_url_path', 'teardown_appcontext', 'teardown_appcontext_funcs', 'teardown_request', 'teardown_request_funcs', 'template_context_processors', 'template_filter', 'template_folder', 'template_global', 'template_test', 'test_client', 'test_client_class', 'test_request_context', 'testing', 'trap_http_exception', 'try_trigger_before_first_request_functions', 'update_template_context', 'url_build_error_handlers', 'url_default_functions', 'url_defaults', 'url_map', 'url_rule_class', 'url_value_preprocessor', 'url_value_preprocessors', 'use_x_sendfile', 'view_functions', 'wsgi_app']

なるへそ。