rewrite extension docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
remove old extension patterns remove flask.ext from api docs
authored by David Lord · committed by David Lord · parent cc6c618 · Browse files · 3 files·+41 added−128 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−128 removed
- other:+41 added−128 removed
Provenance
- Commit
4462cc5- Tree
a627f3c- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
cc6c618
3 files changed+41 added−128 removed
modified:docs/api.rst-16 removed
368 unmodified lines369 370 .. autoclass:: Config371 :members:372 373-Extensions374-----------373 376-.. data:: flask.ext377-378- This module acts as redirect import module to Flask extensions. It was379- added in 0.8 as the canonical way to import Flask extensions and makes380- it possible for us to have more flexibility in how we distribute381- extensions.382-383- If you want to use an extension named “Flask-Foo” you would import it384- from :data:`~flask.ext` as follows::385-386- from flask.ext import foo387-388- .. versionadded:: 0.8389-374 Stream Helpers375 --------------376 377 .. autofunction:: stream_with_context444 unmodified linesmodified:docs/extensiondev.rst+8 added-74 removed
157 unmodified lines158 159 Here's the contents of the `flask_sqlite3.py` for copy/paste::160 161 import sqlite3162- from flask import current_app162+ from flask import current_app, _app_ctx_stack163 164- # Find the stack on which we want to store the database connection.165- # Starting with Flask 0.9, the _app_ctx_stack is the correct one,166- # before that we need to use the _request_ctx_stack.167- try:168- from flask import _app_ctx_stack as stack169- except ImportError:170- from flask import _request_ctx_stack as stack164 172-165 class SQLite3(object):174-166 def __init__(self, app=None):167 self.app = app168 if app is not None:169 self.init_app(app)170 171 def init_app(self, app):172 app.config.setdefault('SQLITE3_DATABASE', ':memory:')182- # Use the newstyle teardown_appcontext if it's available,183- # otherwise fall back to the request context184- if hasattr(app, 'teardown_appcontext'):185- app.teardown_appcontext(self.teardown)173+ app.teardown_appcontext(self.teardown)186- else:187- app.teardown_request(self.teardown)174 175 def connect(self):176 return sqlite3.connect(current_app.config['SQLITE3_DATABASE'])177 178 def teardown(self, exception):193- ctx = stack.top179+ ctx = _app_ctx_stack.top180 if hasattr(ctx, 'sqlite3_db'):181 ctx.sqlite3_db.close()182 183 @property184 def connection(self):199- ctx = stack.top185+ ctx = _app_ctx_stack.top186 if ctx is not None:187 if not hasattr(ctx, 'sqlite3_db'):188 ctx.sqlite3_db = self.connect()189 return ctx.sqlite3_db7 unmodified lines197 instantiated without requiring an app object. This method supports the198 factory pattern for creating applications. The ``init_app`` will set the199 configuration for the database, defaulting to an in memory database if200 no configuration is supplied. In addition, the ``init_app`` method attaches215- the ``teardown`` handler. It will try to use the newstyle app context216- handler and if it does not exist, falls back to the request context217- one.201+ the ``teardown`` handler.202 3. Next, we define a ``connect`` method that opens a database connection.203 4. Finally, we add a ``connection`` property that on first access opens204 the database connection and stores it on the context. This is also205 the recommended way to handling resources: fetch resources lazily the1 unmodified line207 208 Note here that we're attaching our database connection to the top209 application context via ``_app_ctx_stack.top``. Extensions should use210 the top context for storing their own information with a sufficiently227- complex name. Note that we're falling back to the228- ``_request_ctx_stack.top`` if the application is using an older229- version of Flask that does not support it.211+ complex name.212 213 So why did we decide on a class-based approach here? Because using our214 extension looks something like this::215 10 unmodified lines226 def show_all():227 cur = db.connection.cursor()228 cur.execute(...)229 248-Likewise if you are outside of a request but you are using Flask 0.9 or249-later with the app context support, you can use the database in the same250-way::230+Likewise if you are outside of a request you can use the database by231+pushing an app context::232 233 with app.app_context():234 cur = db.connection.cursor()235 cur.execute(...)35 unmodified lines271 teardown of a request, the ``sqlite3_db`` connection is closed. By using272 this pattern, the *same* connection to the sqlite3 database is accessible273 to anything that needs it for the duration of the request.274 294-If the :data:`~flask._app_ctx_stack` does not exist because the user uses295-an old version of Flask, it is recommended to fall back to296-:data:`~flask._request_ctx_stack` which is bound to a request.275 298-Teardown Behavior299------------------300-301-*This is only relevant if you want to support Flask 0.6 and older*302-303-Due to the change in Flask 0.7 regarding functions that are run at the end304-of the request your extension will have to be extra careful there if it305-wants to continue to support older versions of Flask. The following306-pattern is a good way to support both::307-308- def close_connection(response):309- ctx = _request_ctx_stack.top310- ctx.sqlite3_db.close()311- return response312-313- if hasattr(app, 'teardown_request'):314- app.teardown_request(close_connection)315- else:316- app.after_request(close_connection)317-318-Strictly speaking the above code is wrong, because teardown functions are319-passed the exception and typically don't return anything. However because320-the return value is discarded this will just work assuming that the code321-in between does not touch the passed parameter.322-276 Learn from Others277 -----------------278 279 This documentation only touches the bare minimum for extension58 unmodified lines338 even if the extension would be safe for zipping.339 10. An extension currently has to support Python 3.4 and newer and 2.7.340 341 389-390-Extension Import Transition391----------------------------392-393-In early versions of Flask we recommended using namespace packages for Flask394-extensions, of the form ``flaskext.foo``. This turned out to be problematic in395-practice because it meant that multiple ``flaskext`` packages coexist.396-Consequently we have recommended to name extensions ``flask_foo`` over397-``flaskext.foo`` for a long time.398-399-Flask 0.8 introduced a redirect import system as a compatibility aid for app400-developers: Importing ``flask.ext.foo`` would try ``flask_foo`` and401-``flaskext.foo`` in that order.402-403-As of Flask 0.11, most Flask extensions have transitioned to the new naming404-schema. The ``flask.ext.foo`` compatibility alias is still in Flask 0.11 but is405-now deprecated -- you should use ``flask_foo``.406-407-342 .. _OAuth extension: https://pythonhosted.org/Flask-OAuth/343 .. _mailinglist: http://flask.pocoo.org/mailinglist/344 .. _IRC channel: http://flask.pocoo.org/community/irc/modified:docs/extensions.rst+33 added-38 removed
1 .. _extensions:2 3-Flask Extensions4-================3+Extensions4+==========5 6-Flask extensions extend the functionality of Flask in various different7-ways. For instance they add support for databases and other common tasks.6+Extensions are extra packages that add functionality to a Flask7+application. For example, an extension might add support for sending8+email or connecting to a database. Some extensions add entire new9+frameworks to help build certain types of applications, like a ReST API.10 11+12 Finding Extensions13 ------------------14 12-Flask extensions are listed on the `Flask Extension Registry`_ and can be13-downloaded with :command:`easy_install` or :command:`pip`. If you add a Flask extension14-as dependency to your :file:`requirements.txt` or :file:`setup.py` file they are15-usually installed with a simple command or when your application installs.15+Flask extensions are usually named "Flask-Foo" or "Foo-Flask". Many16+extensions are listed in the `Extension Registry`_, which can be updated17+by extension developers. You can also search PyPI for packages tagged18+with `Framework :: Flask <pypi_>`_.19 20+21 Using Extensions22 ----------------23 20-Extensions typically have documentation that goes along that shows how to21-use it. There are no general rules in how extensions are supposed to22-behave but they are imported from common locations. If you have an23-extension called ``Flask-Foo`` or ``Foo-Flask`` it should be always24-importable from ``flask_foo``::24+Consult each extension's documentation for installation, configuration,25+and usage instructions. Generally, extensions pull their own26+configuration from :attr:`app.config <flask.Flask.config>` and are27+passed an application instance during initialization. For example,28+an extension caled "Flask-Foo" might be used like this::29 26- import flask_foo30+ from flask_foo import Foo31 28-Building Extensions29--------------------32+ foo = Foo()33 31-While `Flask Extension Registry`_ contains many Flask extensions, you may not find32-an extension that fits your need. If this is the case, you can always create your own. 33-Consider reading :ref:`extension-dev` to develop your own Flask extension.34+ app = Flask(__name__)35+ app.config.update(36+ FOO_BAR='baz',37+ FOO_SPAM='eggs',38+ )39 35-Flask Before 0.836-----------------40+ foo.init_app(app)41 38-If you are using Flask 0.7 or earlier the :data:`flask.ext` package will not39-exist, instead you have to import from ``flaskext.foo`` or ``flask_foo``40-depending on how the extension is distributed. If you want to develop an41-application that supports Flask 0.7 or earlier you should still import42-from the :data:`flask.ext` package. We provide you with a compatibility43-module that provides this package for older versions of Flask. You can44-download it from GitHub: `flaskext_compat.py`_42 46-And here is how you can use it::43+Building Extensions44+-------------------45 48- import flaskext_compat49- flaskext_compat.activate()46+While the `Extension Registry`_ contains many Flask extensions, you may47+not find an extension that fits your need. If this is the case, you can48+create your own. Read :ref:`extension-dev` to develop your own Flask49+extension.50 51- from flask.ext import foo51 53-Once the ``flaskext_compat`` module is activated the :data:`flask.ext` will54-exist and you can start importing from there.55-56-57-.. _Flask Extension Registry: http://flask.pocoo.org/extensions/58-.. _flaskext_compat.py: https://raw.githubusercontent.com/pallets/flask/master/scripts/flaskext_compat.py52+.. _Extension Registry: http://flask.pocoo.org/extensions/53+.. _pypi: https://pypi.python.org/pypi?:action=browse&c=585