使用create_app工厂模式时如何从FlaskGroup访问Flask-app配置?

时间:2019-11-18 13:22:57

标签: python flask

如何在app.config内访问FlaskGroup

FlaskGroup的设置如下,

cli = FlaskGroup(create_app=create_app)

这样,在已注册的命令中,您可以访问应用程序配置,

@cli.command('run_worker')
def run_worker():
    # 1. This did not provide a meaningful clue
    print(dir(cli))

    # 2. How to access the config without instantiating the app?
    redis_url = app.config['REDIS_URL'] 

供参考,1.,产量

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_load_plugin_commands', '_loaded_plugin_commands', 'add_command', 'add_help_option', 'allow_extra_args', 'allow_interspersed_args', 'callback', 'chain', 'collect_usage_pieces', 'command', 'commands', 'context_settings', 'create_app', 'deprecated', 'epilog', 'format_commands', 'format_epilog', 'format_help', 'format_help_text', 'format_options', 'format_usage', 'get_command', 'get_help', 'get_help_option', 'get_help_option_names', 'get_params', 'get_short_help_str', 'get_usage', 'group', 'help', 'hidden', 'ignore_unknown_options', 'invoke', 'invoke_without_command', 'list_commands', 'load_dotenv', 'main', 'make_context', 'make_parser', 'name', 'no_args_is_help', 'options_metavar', 'params', 'parse_args', 'resolve_command', 'result_callback', 'resultcallback', 'set_debug_flag', 'short_help', 'subcommand_metavar']

我想完成2,最好不必先做cli.create_app

0 个答案:

没有答案