Skip to content
Myr

Documented new import system for extensions

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 1e6c5f0 · Browse files · 1 file+42 added−38 removed
This commit

State

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

Diff stats

1 file+42 added−38 removed

  • other:+42 added−38 removed

1 file+42 added−38 removed

modified:docs/extensiondev.rst+42 added-38 removed
14 unmodified lines
1516Anatomy of an Extension17-----------------------1819Extensions are all located in a package called ``flaskext.something``19Extensions are all located in a package called ``flask_something``20where "something" is the name of the library you want to bridge. So for21example if you plan to add support for a library named `simplexml` to22Flask, you would name your extension's package ``flaskext.simplexml``.22Flask, you would name your extension's package ``flask_simplexml``.2324The name of the actual extension (the human readable name) however would25be something like "Flask-SimpleXML". Make sure to include the name26"Flask" somewhere in that name and that you check the capitalization.27This is how users can then register dependencies to your extension in28their `setup.py` files.2930The magic that makes it possible to have your library in a package called31``flaskext.something`` is called a "namespace package". Check out the32guide below how to create something like that.30Flask sets up a redirect package called :data:`flask.ext` where users31should import the extensions from. If you for instance have a package32called ``flask_something`` users would import it as33``flask.ext.something``. This is done to transition from the old34namespace packages. See :ref:`ext-import-transition` for more details.3536But how do extensions look like themselves? An extension has to ensure37that it works with multiple Flask application instances at once. This is38a requirement because many people will use patterns like the
16 unmodified lines
5556So let's get started with creating such a Flask extension. The extension57we want to create here will provide very basic support for SQLite3.5857There is a script on github called `Flask Extension Wizard`_ which helps58you create the initial folder structure. But for this very basic example59we want to create all by hand to get a better feeling for it.6059First we create the following folder structure::6061 flask-sqlite3/64 flaskext/65 __init__.py66 sqlite3.py67 setup.py62 flask_sqlite3.py63 LICENSE64 README6566Here's the contents of the most important files:6772flaskext/__init__.py73````````````````````7475The only purpose of this file is to mark the package as namespace package.76This is required so that multiple modules from different PyPI packages can77reside in the same Python package::7879 __import__('pkg_resources').declare_namespace(__name__)8081If you want to know exactly what is happening there, checkout the82distribute or setuptools docs which explain how this works.8384Just make sure to not put anything else in there!8568setup.py69````````7071The next file that is absolutely required is the `setup.py` file which is
17 unmodified lines
89 author='Your Name',90 author_email='your-email@example.com',91 description='Very short description',92 long_description=__doc__,111 packages=['flaskext'],112 namespace_packages=['flaskext'],93 py_modules=['flask_sqlite3'],94 # if you would be using a package instead use packages instead95 # of py_modules:96 # packages=['flask_sqlite3'],97 zip_safe=False,98 include_package_data=True,99 platforms='any',100 install_requires=[101 'Flask'102 ],
8 unmodified lines
111 ]112 )113114That's a lot of code but you can really just copy/paste that from existing130extensions and adapt. This is also what the wizard creates for you if you131use it.115extensions and adapt.116133flaskext/sqlite3.py134```````````````````117flask_sqlite3.py118````````````````119120Now this is where your extension code goes. But how exactly should such121an extension look like? What are the best practices? Continue reading122for some insight.
30 unmodified lines
153154The Extension Code155------------------156173Here's the contents of the `flaskext/sqlite3.py` for copy/paste::157Here's the contents of the `flask_sqlite3.py` for copy/paste::158159 from __future__ import absolute_import160 import sqlite3161
44 unmodified lines
206So why did we decide on a class based approach here? Because using our207extension looks something like this::208209 from flask import Flask226 from flaskext.sqlite3 import SQLite3210 from flask_sqlite3 import SQLite3211212 app = Flask(__name__)213 app.config.from_pyfile('the-config.cfg')214 manager = SQLite3(app)
111 unmodified lines
326Extension Registry`_ and marked appropriately. If you want your own327extension to be approved you have to follow these guidelines:3283291. An approved Flask extension must provide exactly one package or module346 inside the `flaskext` namespace package.330 named ``flask_extensionname``. They might also reside inside a331 ``flaskext`` namespace packages though this is discouraged now.3322. It must ship a testing suite that can either be invoked with ``make test``333 or ``python setup.py test``. For test suites invoked with ``make334 test`` the extension has to ensure that all dependencies for the test335 are installed automatically, in case of ``python setup.py test``
24 unmodified lines
36010. An extension currently has to support Python 2.5, 2.6 as well as361 Python 2.7362363379.. _Flask Extension Wizard:380 http://github.com/mitsuhiko/flask-extension-wizard364.. _ext-import-transition:365366Extension Import Transition367---------------------------368369For a while we recommended using namespace packages for Flask extensions.370This turned out to be problematic in practice because many different371competing namespace package systems exist and pip would automatically372switch between different systems and this caused a lot of problems for373users.374375Instead we now recommend naming packages ``flask_foo`` instead of the now376deprecated ``flaskext.foo``. Flask 0.8 introduces a redirect import377system that lets uses import from ``flask.ext.foo`` and it will try378``flask_foo`` first and if that fails ``flaskext.foo``.379380Flask extensions should urge users to import from ``flask.ext.foo``381instead of ``flask_foo`` or ``flaskext_foo`` so that extensions can382transition to the new package name without affecting users.383384385.. _OAuth extension: http://packages.python.org/Flask-OAuth/386.. _mailinglist: http://flask.pocoo.org/mailinglist/387.. _IRC channel: http://flask.pocoo.org/community/irc/