mirror of https://github.com/mitsuhiko/flask.git
David Lord
7 years ago
3 changed files with 41 additions and 128 deletions
@ -1,58 +1,53 @@ |
|||||||
.. _extensions: |
.. _extensions: |
||||||
|
|
||||||
Flask Extensions |
Extensions |
||||||
================ |
========== |
||||||
|
|
||||||
|
Extensions are extra packages that add functionality to a Flask |
||||||
|
application. For example, an extension might add support for sending |
||||||
|
email or connecting to a database. Some extensions add entire new |
||||||
|
frameworks to help build certain types of applications, like a ReST API. |
||||||
|
|
||||||
Flask extensions extend the functionality of Flask in various different |
|
||||||
ways. For instance they add support for databases and other common tasks. |
|
||||||
|
|
||||||
Finding Extensions |
Finding Extensions |
||||||
------------------ |
------------------ |
||||||
|
|
||||||
Flask extensions are listed on the `Flask Extension Registry`_ and can be |
Flask extensions are usually named "Flask-Foo" or "Foo-Flask". Many |
||||||
downloaded with :command:`easy_install` or :command:`pip`. If you add a Flask extension |
extensions are listed in the `Extension Registry`_, which can be updated |
||||||
as dependency to your :file:`requirements.txt` or :file:`setup.py` file they are |
by extension developers. You can also search PyPI for packages tagged |
||||||
usually installed with a simple command or when your application installs. |
with `Framework :: Flask <pypi_>`_. |
||||||
|
|
||||||
|
|
||||||
Using Extensions |
Using Extensions |
||||||
---------------- |
---------------- |
||||||
|
|
||||||
Extensions typically have documentation that goes along that shows how to |
Consult each extension's documentation for installation, configuration, |
||||||
use it. There are no general rules in how extensions are supposed to |
and usage instructions. Generally, extensions pull their own |
||||||
behave but they are imported from common locations. If you have an |
configuration from :attr:`app.config <flask.Flask.config>` and are |
||||||
extension called ``Flask-Foo`` or ``Foo-Flask`` it should be always |
passed an application instance during initialization. For example, |
||||||
importable from ``flask_foo``:: |
an extension caled "Flask-Foo" might be used like this:: |
||||||
|
|
||||||
import flask_foo |
from flask_foo import Foo |
||||||
|
|
||||||
Building Extensions |
foo = Foo() |
||||||
------------------- |
|
||||||
|
|
||||||
While `Flask Extension Registry`_ contains many Flask extensions, you may not find |
app = Flask(__name__) |
||||||
an extension that fits your need. If this is the case, you can always create your own. |
app.config.update( |
||||||
Consider reading :ref:`extension-dev` to develop your own Flask extension. |
FOO_BAR='baz', |
||||||
|
FOO_SPAM='eggs', |
||||||
|
) |
||||||
|
|
||||||
Flask Before 0.8 |
foo.init_app(app) |
||||||
---------------- |
|
||||||
|
|
||||||
If you are using Flask 0.7 or earlier the :data:`flask.ext` package will not |
|
||||||
exist, instead you have to import from ``flaskext.foo`` or ``flask_foo`` |
|
||||||
depending on how the extension is distributed. If you want to develop an |
|
||||||
application that supports Flask 0.7 or earlier you should still import |
|
||||||
from the :data:`flask.ext` package. We provide you with a compatibility |
|
||||||
module that provides this package for older versions of Flask. You can |
|
||||||
download it from GitHub: `flaskext_compat.py`_ |
|
||||||
|
|
||||||
And here is how you can use it:: |
Building Extensions |
||||||
|
------------------- |
||||||
import flaskext_compat |
|
||||||
flaskext_compat.activate() |
|
||||||
|
|
||||||
from flask.ext import foo |
|
||||||
|
|
||||||
Once the ``flaskext_compat`` module is activated the :data:`flask.ext` will |
While the `Extension Registry`_ contains many Flask extensions, you may |
||||||
exist and you can start importing from there. |
not find an extension that fits your need. If this is the case, you can |
||||||
|
create your own. Read :ref:`extension-dev` to develop your own Flask |
||||||
|
extension. |
||||||
|
|
||||||
|
|
||||||
.. _Flask Extension Registry: http://flask.pocoo.org/extensions/ |
.. _Extension Registry: http://flask.pocoo.org/extensions/ |
||||||
.. _flaskext_compat.py: https://raw.githubusercontent.com/pallets/flask/master/scripts/flaskext_compat.py |
.. _pypi: https://pypi.python.org/pypi?:action=browse&c=585 |
||||||
|
Loading…
Reference in new issue