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. Linux挂载Windows共享目录

    在windows中设置共享目录并添加权限用户 把Window系统的文件共享挂载到linux centos 目录下的方法步骤: 1.先在windows下面共享需要挂载的目录. 2.确保linux与win ...

  2. Squid 正向代理配置

    Squid 正向代理配置 1.删除主配置文件重写写入配置 rm -f /etc/squid/squid.conf 2.重新写入配置正向代理 vim /etc/squid/squid.conf # 监听 ...

  3. 时间插件之My97DatePickerBeta

    My97DatePickerBeta使用很简单 1.在jsp页面中 引入JS 下载地址:链接: https://pan.baidu.com/s/1bp5uzuv 密码: ya9a <script ...

  4. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  5. Linux FTP 上传一键脚本

    下面来介绍一下这个 FTP 上传一键脚本 ftp_upload.sh. 用途:用于在Linux系统下搭建FTP客户端向FTP服务器端上传文件: 总结一下 ftp_upload.sh 特点:1.支持文件 ...

  6. 阿里云环境搭建CDN内容分发

    1.创建CDN CNAME 指向CDN云厂商地址 2.使用域名转向到CDN云厂商地址 请求过来 通过cnd 分发到不同的服务器  如果有缓存的话 直接走了 CDN也可以实现安全功能,比如CDN实现防止 ...

  7. java中,return和return null有什么区别吗?

    java中,return和return null有什么区别吗? 最大的区别:return;方法的返回值必须是void!return null;方法的返回值必须不是 原始数据类型(封装类除过)和void ...

  8. DelphiXE_Android

    1. http://download.csdn.net/detail/gx15941883020/8104761 2. http://bbs.2ccc.com/topic.asp?topicid=45 ...

  9. 10.0.4_CentOS_120g

    对应 VMware Workstation 版本为:“10.0.4 build-2249910”

  10. 判断A字符串是B字符串的反转

    先将其中一个字符串进行反转操作,然后两个字符串进行判断. 1.反转 /** * 字符串反转 * @param str * @return */ private static String conver ...