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()
    
    

flask之instance_path实例路径的更多相关文章

  1. openstack nova修改实例路径,虚拟磁盘路径

    #实例路径 --instances_path=$state_path/instances #日志的目录 --logdir=/var/log/nova #nova的目录 --state_path=/va ...

  2. Flask之app实例的参数配置

    说是app实例的配置, 实际也就是flask程序的配置 Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? ...

  3. Flask中获取参数(路径,查询,请求体,请求头)

    上一篇中已经讲述了:HTTP协议向服务器传参有几种途径{ 链接 } 在Flask中同样通过这4中传参途径进行归纳: 1. URL中路径参数的获取: 拓展: # 路由参数/路径参数:http://127 ...

  4. Flask基础-基础实例

    1. 10行代码的迷你程序 flask项目 from flask import Flask app = Flask(__name__) @app.route("/index") d ...

  5. Flask 架构 --xunfeng实例研究

    文件结构 │ Config.py # 配置文件 │ README.md # 说明文档 │ Run.bat # Windows启动服务 │ Run.py # webserver │ Run.sh # L ...

  6. 基于python的flask的应用实例注意事项

    1.所有的html文件均保存在templates文件夹中 2.运行网页时python manage.py runserver

  7. Android实例-路径信息及文件和文件夹的操作(XE8+小米2)

    结果: GetTempFileName:/storage/sdcard0/Android/data/com.embarcadero.Project1/files/tmp/tmp.iQIip24407 ...

  8. Flask实战-留言板-使用Flask-DebugToolbar调试程序、Flask配置的两种组织形式

    使用Flask-DebugToolbar调试程序 扩展Flask-DebugToolbar提供了一系列调试功能,可以用来查看请求的SQL语句.配置选项.资源加载情况等信息.这些信息在开发时会非常有用. ...

  9. Flask 参数简介

    我们都知道学习了Flask的时候它里面的参数是有很多种的参数  都是需要相互进行调用传递的  今天就简要分析一些常见的参数 首先导入Flask之后看 源码 from flask import Flas ...

随机推荐

  1. Python编程-架构、Socket

    一.客户端/服务器架构 1.C/S架构 Client/Server架构,即服务器/客户端架构. 客户端和服务器端的程序不同,用户的程序主要在客户端,服务器端主要提供数据管理.数据共享.数据及系统维护和 ...

  2. 从引物序列出发查找pcr产物的内容和在基因组上的位置

    1.利用primer_blast工具,找出这对引物序列在基因组上的位置: 结果大概会像这样: 2.这些结果都是根据hg38基因组来定位的,转换成hg19: 利用UCSCde hgLiftover 在线 ...

  3. INSPIRED启示录 读书笔记 - 第23章 改进现有产品

    不是一味地添加功能 改进产品不是简单地满足个别用户的要求,也不能对用户调查的结果照单全收.能提高指标的功能才是关注的重点.应该找准方向,分析关键指标,有针对性地改进产品

  4. Centos7 关闭Ipv6

  5. CCNA 课程 四

    Vlan基础: Vlan的作用:把物理上分割的用户,让他们逻辑上在一起. Vlan 范围: 0- 4095 0  4095 是保留的 不可以使用 1 cisco 本证vlan 标准vlan 1 -10 ...

  6. python批量修改文件名称

    参考文章:http://www.cnblogs.com/ma6174/archive/2012/05/04/2482378.html 最近遇到一个问题,在网上下载了一批视频课程,需要将每节课的名称标号 ...

  7. 什么是CDN 域名预热

    通常大家理解的预热功能是,缓存预热.将需要预热的资源推送到CDN边缘节点,用户访问时将不需要再回源站,可以直接命中缓存.这样可以加快资源的首次访问,同时缓解突增回源流量给源站造成的压力.这里说的是域名 ...

  8. Linux嵌入式 -- 内核 - 进程控制 和 调度

    1. 进程四要素 1. 有一段程序供其执行.这段程序不一定是某个进程所专有,可以与其他进程共用. 2. 有进程专用的内核空间堆栈. 3. 在内核中有一个task_struct数据结构,即通常所说的&q ...

  9. Composer如何安装(安装注意事项)

    Composer如何安装(安装注意事项) 一.总结 一句话总结:安装的时候主要看安装错误提示: 常见的错误有: a.php需要开启openssl配置.我们打开php目录下的php.ini.将opens ...

  10. CSS基础(float属性与清除浮动)

    3.8 这是CSS里比较重要的属性:浮动,这个属性会在以后经常用到,算是一个重点吧 浮动 语法:float:left  |  right  |  none 特点: 浮动的元素不占位置,脱离了标准文档流 ...