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
/instanceUninstalled package:
/myapp
/__init__.py
/instanceSince 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 withFlask.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实例路径的更多相关文章
- openstack nova修改实例路径,虚拟磁盘路径
#实例路径 --instances_path=$state_path/instances #日志的目录 --logdir=/var/log/nova #nova的目录 --state_path=/va ...
- Flask之app实例的参数配置
说是app实例的配置, 实际也就是flask程序的配置 Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? ...
- Flask中获取参数(路径,查询,请求体,请求头)
上一篇中已经讲述了:HTTP协议向服务器传参有几种途径{ 链接 } 在Flask中同样通过这4中传参途径进行归纳: 1. URL中路径参数的获取: 拓展: # 路由参数/路径参数:http://127 ...
- Flask基础-基础实例
1. 10行代码的迷你程序 flask项目 from flask import Flask app = Flask(__name__) @app.route("/index") d ...
- Flask 架构 --xunfeng实例研究
文件结构 │ Config.py # 配置文件 │ README.md # 说明文档 │ Run.bat # Windows启动服务 │ Run.py # webserver │ Run.sh # L ...
- 基于python的flask的应用实例注意事项
1.所有的html文件均保存在templates文件夹中 2.运行网页时python manage.py runserver
- Android实例-路径信息及文件和文件夹的操作(XE8+小米2)
结果: GetTempFileName:/storage/sdcard0/Android/data/com.embarcadero.Project1/files/tmp/tmp.iQIip24407 ...
- Flask实战-留言板-使用Flask-DebugToolbar调试程序、Flask配置的两种组织形式
使用Flask-DebugToolbar调试程序 扩展Flask-DebugToolbar提供了一系列调试功能,可以用来查看请求的SQL语句.配置选项.资源加载情况等信息.这些信息在开发时会非常有用. ...
- Flask 参数简介
我们都知道学习了Flask的时候它里面的参数是有很多种的参数 都是需要相互进行调用传递的 今天就简要分析一些常见的参数 首先导入Flask之后看 源码 from flask import Flas ...
随机推荐
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- Windos Server Tomcat 双开配置
Tomcat 双开配置 tomcat_1 server.mxl文件 # 修改端口 <Connector port=" protocol="HTTP/1.1" c ...
- Kubernetes Heapster
Heapster是容器集群监控和性能分析工具,HPA.Dashborad.Kubectl top都依赖于heapster收集的数据. 但是Heapster从kubernetes 1.8以后已经被遗弃了 ...
- CentOS 7 导入epel库
yum install epel-release 或者到百度云下载相应的 rpm 包进行安装 rpm -ivh epel-release-7-9.noarch.rpm
- P2610 【[ZJOI2012]旅游】(dfs+树的直径)
楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图 ...
- 小组E-R图的改进 2.0版本
上图是我对我们小组的数据库E-R图的改进 我在出勤上加了一个学生留言,因为我们组在需求分析中说明了有个教师与学生通过此软件交流的功能,教师可以通过这个功能给学生留言,学生也可以通过此功能请假或者说明旷 ...
- wareshark网络协议分析之ARP
一.ARP协议简介 简单的说ARP协议就是实现ip地址到物理地址的映射.当一台主机把以太网数据帧发送到位于同一局域网上的另一台主机时,是根据48bit的以太网地址(物理地址)来确定网络接口的. ARP ...
- MapReduce-join连接
join连接 MapReduce能够执行大型数据集间的连接(join)操作.连接操作的具体实现技术取决于数据集的规模及分区方式连接操作如果由mapper执行,则称为“map端连接”:如果由reduce ...
- 1、html(),text(),value()的区别;2、remove() 3、加载完执行函数
1 .html()用为读取和修改元素的HTML标签.2 .text()用来读取或修改元素的纯文本内容.3 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html() ...
- webservice 交错数组
net webservices public DataSet SelectOPQuestionByWhere(string strWhere, string[][] strArry) { if (!k ...