gpt4 book ai didi

python - 如何根据我的项目结构访问应用程序上下文之外的 flask 配置变量?

转载 作者:行者123 更新时间:2023-12-03 23:15:42 25 4
gpt4 key购买 nike

spinngod.py -flask 应用程序启动代码

from app import create_app
import sys

run_profile = str(sys.argv[1]) if len(sys.argv) >= 2 else 'development'
app = create_app(run_profile)

print("App Root Path:" + app.root_path)

if __name__ == '__main__':
print sys.path
app.run(debug=True, host='0.0.0.0')

应用程序/ 初始化 .py - 创建 flask 应用程序
def create_app(profile_name):
print "currently active profile:" + profile_name
app = Flask(__name__)

############# configurations ####################
app.config.from_object(config[profile_name])
configure_app(app)
configure_app_logger(app)

#################### blueprint registration and rest_plus namespace additions ###############
from api_1_0 import api as api_1_0_blueprint
from api_1_0.restplus import api_restplus

# ************************************************** #
api_restplus.init_app(api_1_0_blueprint)
api_restplus.add_namespace(application_namespace)
api_restplus.add_namespace(pipeline_template_namespace)
api_restplus.add_namespace(loadbalancer_namespace)
api_restplus.add_namespace(servergroup_namespace)
api_restplus.add_namespace(task_namespace)
# ************************************************** #

app.register_blueprint(api_1_0_blueprint)

##############################################################
return app

我想在应用程序上下文之外的其他一些文件中访问 c​​onfig.py 中定义的 flask 配置变量。应用程序配置取决于从命令行作为参数传递的配置文件(开发、阶段或生产)。

我能想到的在应用程序上下文之外访问配置变量的唯一方法是将配置文件(dev、stage 或 prod)设置为环境变量和
然后直接从配置文件导入。

我尝试的第二种方法是在 app/ 中移动 flask 应用程序的创建。初始化 .py 外部方法。

这就是我尝试访问另一个类中的配置变量的方式。
import requests


class Client(object):
def __init__(self):
from app import app
print "fjaijflkajsf" + app.config['SPINNAKER_BASE_URL']
pass

有没有更好的方法在 flask 中做到这一点?

最佳答案

来自 docs :

Rather than passing the application around to each function, the current_app and g proxies are accessed instead.

The Flask application object has attributes, such as config, that are useful to access within views and CLI commands. However, importing the app instance within the modules in your project is prone to circular import issues.

Flask solves this issue with the application context. Rather than referring to an app directly, you use the the current_app proxy, which points to the application handling the current activity.


你像这样导入 current_app :
from flask import current_app
然后像这样访问配置或其他属性:
config = current_app.config
例子:
src/application.py(其中 config 在上下文中设置)
create_app():
app = Flask('app')
app.config.from_object(some_class)
return app
src/module/another_module.py
from flask import current_app

def function_that_requires_config():
config = current_app.config
选择:
src/application.py(其中 config 在上下文中设置)
APP = create_app(os.environ.get('FLASK_ENV'))
src/module/another_module.py
from src.application import APP

def function_that_requires_config():
config_value = APP.config.get(config_key, default_value)

关于python - 如何根据我的项目结构访问应用程序上下文之外的 flask 配置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com