使用创建应用程序工厂模式时,如何从FlaskGroup访问Flaskapp配置?

2024-09-25 00:36:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何访问FlaskGroup中的app.config

{}是这样设置的

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


Tags: runconfigappformatgetclicreatehelp