zoukankan      html  css  js  c++  java
  • flask之instance_path实例路径

    Flask 0.8 introduces instance folders. Flask for a long time made it possible to refer to paths relative to the application’s folder directly (via Flask.root_path). This was also how many developers loaded configurations stored next to the application. Unfortunately however this only works well if applications are not packages in which case the root path refers to the contents of the package.

    With Flask 0.8 a new attribute was introduced: Flask.instance_path. It refers to a new concept called the “instance folder”. The instance folder is designed to not be under version control and be deployment specific. It’s the perfect place to drop things that either change at runtime or configuration files.

    You can either explicitly provide the path of the instance folder when creating the Flask application or you can let Flask autodetect the instance folder. For explicit configuration use the instance_path parameter:

    app = Flask(__name__, instance_path='/path/to/instance/folder')

    Please keep in mind that this path must be absolute when provided.

    If the instance_path parameter is not provided the following default locations are used:

    • Uninstalled module:

      /myapp.py
      /instance
      
    • Uninstalled package:

      /myapp
          /__init__.py
      /instance

      Since the config object provided loading of configuration files from relative filenames we made it possible to change the loading via filenames to be relative to the instance path if wanted. The behavior of relative paths in config files can be flipped between “relative to the application root” (the default) to “relative to instance folder” via the instance_relative_config switch to the application constructor:

      app = Flask(__name__, instance_relative_config=True)
      

      Here is a full example of how to configure Flask to preload the config from a module and then override the config from a file in the config folder if it exists:

      app = Flask(__name__, instance_relative_config=True)
      app.config.from_object('yourapplication.default_settings')
      app.config.from_pyfile('application.cfg', silent=True)

      The path to the instance folder can be found via the Flask.instance_path. Flask also provides a shortcut to open a file from the instance folder with Flask.open_instance_resource().

      Example usage for both:

      filename = os.path.join(app.instance_path, 'application.cfg')
      with open(filename) as f:
          config = f.read()
      
      # or via open_instance_resource:
      with app.open_instance_resource('application.cfg') as f:
          config = f.read()
      

  • 相关阅读:
    【Codeforces 776B】Sherlock and his girlfriend
    BZOJ4942 NOI2017整数(线段树)
    BZOJ4516 SDOI2016生成魔咒(后缀数组+平衡树)
    BZOJ4943 NOI2017蚯蚓排队(哈希+链表)
    Codeforces Round#500 Div.2 翻车记
    BZOJ5093 图的价值(NTT+斯特林数)
    BZOJ2821 作诗(分块)
    BZOJ2724 [Violet]蒲公英(分块)
    BZOJ2001 HNOI2010城市建设(线段树分治+LCT)
    BZOJ1093 ZJOI2007最大半连通子图(缩点+dp)
  • 原文地址:https://www.cnblogs.com/zxpo/p/9651004.html
Copyright © 2011-2022 走看看