Skip to content
Myr

Merge pull request #2647 from pallets/docs-extensions

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

remove old extension docs

authored by David Lord · committed by GitHub · parents cc6c618, 4462cc5 · 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

3 files+41 added−128 removed

modified:docs/api.rst-16 removed
368 unmodified lines
369370.. autoclass:: Config371 :members:372373Extensions374----------373376.. data:: flask.ext377378 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.382383 If you want to use an extension named “Flask-Foo” you would import it384 from :data:`~flask.ext` as follows::385386 from flask.ext import foo387388 .. versionadded:: 0.8389374Stream Helpers375--------------376377.. autofunction:: stream_with_context
444 unmodified lines
modified:docs/extensiondev.rst+8 added-74 removed
157 unmodified lines
158159Here's the contents of the `flask_sqlite3.py` for copy/paste::160161 import sqlite3162 from flask import current_app162 from flask import current_app, _app_ctx_stack163164 # 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 stack164172165 class SQLite3(object):174166 def __init__(self, app=None):167 self.app = app168 if app is not None:169 self.init_app(app)170171 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)174175 def connect(self):176 return sqlite3.connect(current_app.config['SQLITE3_DATABASE'])177178 def teardown(self, exception):193 ctx = stack.top179 ctx = _app_ctx_stack.top180 if hasattr(ctx, 'sqlite3_db'):181 ctx.sqlite3_db.close()182183 @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_db
7 unmodified lines
197 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.2023. Next, we define a ``connect`` method that opens a database connection.2034. 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 the
1 unmodified line
207208 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.212213So why did we decide on a class-based approach here? Because using our214extension looks something like this::215
10 unmodified lines
226 def show_all():227 cur = db.connection.cursor()228 cur.execute(...)229248Likewise if you are outside of a request but you are using Flask 0.9 or249later with the app context support, you can use the database in the same250way::230Likewise if you are outside of a request you can use the database by231pushing an app context::232233 with app.app_context():234 cur = db.connection.cursor()235 cur.execute(...)
35 unmodified lines
271teardown of a request, the ``sqlite3_db`` connection is closed. By using272this pattern, the *same* connection to the sqlite3 database is accessible273to anything that needs it for the duration of the request.274294If the :data:`~flask._app_ctx_stack` does not exist because the user uses295an old version of Flask, it is recommended to fall back to296:data:`~flask._request_ctx_stack` which is bound to a request.275298Teardown Behavior299-----------------300301*This is only relevant if you want to support Flask 0.6 and older*302303Due to the change in Flask 0.7 regarding functions that are run at the end304of the request your extension will have to be extra careful there if it305wants to continue to support older versions of Flask. The following306pattern is a good way to support both::307308 def close_connection(response):309 ctx = _request_ctx_stack.top310 ctx.sqlite3_db.close()311 return response312313 if hasattr(app, 'teardown_request'):314 app.teardown_request(close_connection)315 else:316 app.after_request(close_connection)317318Strictly speaking the above code is wrong, because teardown functions are319passed the exception and typically don't return anything. However because320the return value is discarded this will just work assuming that the code321in between does not touch the passed parameter.322276Learn from Others277-----------------278279This documentation only touches the bare minimum for extension
58 unmodified lines
338 even if the extension would be safe for zipping.33910. An extension currently has to support Python 3.4 and newer and 2.7.340341389390Extension Import Transition391---------------------------392393In early versions of Flask we recommended using namespace packages for Flask394extensions, of the form ``flaskext.foo``. This turned out to be problematic in395practice because it meant that multiple ``flaskext`` packages coexist.396Consequently we have recommended to name extensions ``flask_foo`` over397``flaskext.foo`` for a long time.398399Flask 0.8 introduced a redirect import system as a compatibility aid for app400developers: Importing ``flask.ext.foo`` would try ``flask_foo`` and401``flaskext.foo`` in that order.402403As of Flask 0.11, most Flask extensions have transitioned to the new naming404schema. The ``flask.ext.foo`` compatibility alias is still in Flask 0.11 but is405now deprecated -- you should use ``flask_foo``.406407342.. _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:23Flask Extensions4================3Extensions4==========56Flask extensions extend the functionality of Flask in various different7ways. For instance they add support for databases and other common tasks.6Extensions are extra packages that add functionality to a Flask7application. For example, an extension might add support for sending8email or connecting to a database. Some extensions add entire new9frameworks to help build certain types of applications, like a ReST API.101112Finding Extensions13------------------1412Flask extensions are listed on the `Flask Extension Registry`_ and can be13downloaded with :command:`easy_install` or :command:`pip`. If you add a Flask extension14as dependency to your :file:`requirements.txt` or :file:`setup.py` file they are15usually installed with a simple command or when your application installs.15Flask extensions are usually named "Flask-Foo" or "Foo-Flask". Many16extensions are listed in the `Extension Registry`_, which can be updated17by extension developers. You can also search PyPI for packages tagged18with `Framework :: Flask <pypi_>`_.192021Using Extensions22----------------2320Extensions typically have documentation that goes along that shows how to21use it. There are no general rules in how extensions are supposed to22behave but they are imported from common locations. If you have an23extension called ``Flask-Foo`` or ``Foo-Flask`` it should be always24importable from ``flask_foo``::24Consult each extension's documentation for installation, configuration,25and usage instructions. Generally, extensions pull their own26configuration from :attr:`app.config <flask.Flask.config>` and are27passed an application instance during initialization. For example,28an extension caled "Flask-Foo" might be used like this::2926 import flask_foo30 from flask_foo import Foo3128Building Extensions29-------------------32 foo = Foo()3331While `Flask Extension Registry`_ contains many Flask extensions, you may not find32an extension that fits your need. If this is the case, you can always create your own. 33Consider 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 )3935Flask Before 0.836----------------40 foo.init_app(app)4138If you are using Flask 0.7 or earlier the :data:`flask.ext` package will not39exist, instead you have to import from ``flaskext.foo`` or ``flask_foo``40depending on how the extension is distributed. If you want to develop an41application that supports Flask 0.7 or earlier you should still import42from the :data:`flask.ext` package. We provide you with a compatibility43module that provides this package for older versions of Flask. You can44download it from GitHub: `flaskext_compat.py`_4246And here is how you can use it::43Building Extensions44-------------------4548 import flaskext_compat49 flaskext_compat.activate()46While the `Extension Registry`_ contains many Flask extensions, you may47not find an extension that fits your need. If this is the case, you can48create your own. Read :ref:`extension-dev` to develop your own Flask49extension.5051 from flask.ext import foo5153Once the ``flaskext_compat`` module is activated the :data:`flask.ext` will54exist and you can start importing from there.555657.. _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