Skip to content
Myr

Documented instance root

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 153ecbc · Browse files · 4 files+78 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+78 added−3 removed

  • other:+78 added−3 removed

4 files+78 added−3 removed

modified:docs/config.rst+61 added
265 unmodified lines
266 details about how to do that, head over to the267 :ref:`fabric-deployment` pattern.268269.. _fabric: http://fabfile.org/270271272.. _instance-folders:273274Instance Folders275----------------276277.. versionadded:: 0.8278279Flask 0.8 introduces instance folders. Flask for a long time made it280possible to refer to paths relative to the application's folder directly281(via :attr:`Flask.root_path`). This was also how many developers loaded282configurations stored next to the application. Unfortunately however this283only works well if applications are not packages in which case the root284path refers to the contents of the package.285286With Flask 0.8 a new attribute was introduced:287:attr:`Flask.instance_path`. It refers to a new concept called the288“instance folder”. The instance folder is designed to not be under289version control and be deployment specific. It's the perfect place to290drop things that either change at runtime or configuration files.291292To make it easier to put this folder into an ignore list for your version293control system it's called ``instance`` and placed directly next to your294package or module by default. This path can be overridden by specifying295the `instance_path` parameter to your application::296297 app = Flask(__name__, instance_path='/path/to/instance')298299Please keep in mind that this path *must* be absolute when provided.300301Since the config object provided loading of configuration files from302relative filenames we made it possible to change the loading via filenames303to be relative to the instance path if wanted. The behavior of relative304paths in config files can be flipped between “relative to the application305root” (the default) to “relative to instance folder” via the306`instance_relative_config` switch to the application constructor::307308 app = Flask(__name__, instance_relative_config=True)309310Here is a full example of how to configure Flask to preload the config311from a module and then override the config from a file in the config312folder if it exists::313314 app = Flask(__name__, instance_relative_config=True)315 app.config.from_object('yourapplication.default_settings')316 app.config.from_pyfile('application.cfg', silent=True)317318The path to the instance folder can be found via the319:attr:`Flask.instance_path`. Flask also provides a shortcut to open a320file from the instnace folder with :meth:`Flask.open_instance_resource`.321322Example usage for both::323324 filename = os.path.join(app.instance_root, 'application.cfg')325 with open(filename) as f:326 config = f.read()327328 # or via open_instance_resource:329 with app.open_instance_resource('application.cfg') as f:330 config = f.read()
modified:flask/app.py+11 added
552 unmodified lines
553 instance_path = os.path.dirname(root_mod.__file__)554 basedir = os.path.normpath(os.path.abspath(instance_path))555 return os.path.join(basedir, 'instance')556557 def open_instance_resource(self, resource, mode='rb'):558 """Opens a resource from the application's instance folder559 (:attr:`instance_path`). Otherwise works like560 :meth:`open_resource`. Instance resources can also be opened for561 writing.562563 :param resource: the name of the resource. To access resources within564 subfolders use forward slashes as separator.565 """566 return open(os.path.join(self.instance_path, resource), mode)567568 def create_jinja_environment(self):569 """Creates the Jinja2 environment based on :attr:`jinja_options`570 and :meth:`select_jinja_autoescape`. Since 0.7 this also adds571 the Jinja2 globals and filters after initialization. Override
952 unmodified lines
modified:flask/helpers.py+4 added-2 removed
563 unmodified lines
564 if not self.has_static_folder:565 raise RuntimeError('No static folder for this object')566 return send_from_directory(self.static_folder, filename)567568 def open_resource(self, resource):568 def open_resource(self, resource, mode='rb'):569 """Opens a resource from the application's resource folder. To see570 how this works, consider the following folder structure::571572 /myapplication.py
13 unmodified lines
586587 :param resource: the name of the resource. To access resources within588 subfolders use forward slashes as separator.589 """590 if mode not in ('r', 'rb'):591 raise ValueError('Resources can only be opened for reading')590 return open(os.path.join(self.root_path, resource), 'rb')592 return open(os.path.join(self.root_path, resource), mode)
modified:CHANGES+2 added-1 removed
28 unmodified lines
29- Applications now not only have a root path where the resources and modules30 are located but also an instane path which is the designated place to31 drop files that are modified at runtime (uploads etc.). Also this is32 conceptionally only instance depending and outside version control so it's33 the perfect place to put configuration files etc.33 the perfect place to put configuration files etc. For more information34 see :ref:`instance-folders`.3536Version 0.7.337-------------38
238 unmodified lines