Flask Extension Development

Flask, being a microframework, often requires some repetitive steps to geta third party library working. Many such extensions are already availableon PyPI.

If you want to create your own Flask extension for something that does notexist yet, this guide to extension development will help you get yourextension running in no time and to feel like users would expect yourextension to behave.

Anatomy of an Extension

Extensions are all located in a package called flasksomethingwhere “something” is the name of the library you want to bridge. So forexample if you plan to add support for a library named _simplexml toFlask, you would name your extension’s package flask_simplexml.

The name of the actual extension (the human readable name) however wouldbe something like “Flask-SimpleXML”. Make sure to include the name“Flask” somewhere in that name and that you check the capitalization.This is how users can then register dependencies to your extension intheir setup.py files.

But what do extensions look like themselves? An extension has to ensurethat it works with multiple Flask application instances at once. This isa requirement because many people will use patterns like theApplication Factories pattern to create their application as needed to aidunittests and to support multiple configurations. Because of that it iscrucial that your application supports that kind of behavior.

Most importantly the extension must be shipped with a setup.py file andregistered on PyPI. Also the development checkout link should work sothat people can easily install the development version into theirvirtualenv without having to download the library by hand.

Flask extensions must be licensed under a BSD, MIT or more liberal licensein order to be listed in the Flask Extension Registry. Keep in mindthat the Flask Extension Registry is a moderated place and libraries willbe reviewed upfront if they behave as required.

“Hello Flaskext!”

So let’s get started with creating such a Flask extension. The extensionwe want to create here will provide very basic support for SQLite3.

First we create the following folder structure:

  1. flask-sqlite3/
  2. flask_sqlite3.py
  3. LICENSE
  4. README

Here’s the contents of the most important files:

setup.py

The next file that is absolutely required is the setup.py file which isused to install your Flask extension. The following contents aresomething you can work with:

  1. """
  2. Flask-SQLite3
  3. -------------
  4.  
  5. This is the description for that library
  6. """
  7. from setuptools import setup
  8.  
  9.  
  10. setup(
  11. name='Flask-SQLite3',
  12. version='1.0',
  13. url='http://example.com/flask-sqlite3/',
  14. license='BSD',
  15. author='Your Name',
  16. author_email='[email protected]',
  17. description='Very short description',
  18. long_description=__doc__,
  19. py_modules=['flask_sqlite3'],
  20. # if you would be using a package instead use packages instead
  21. # of py_modules:
  22. # packages=['flask_sqlite3'],
  23. zip_safe=False,
  24. include_package_data=True,
  25. platforms='any',
  26. install_requires=[
  27. 'Flask'
  28. ],
  29. classifiers=[
  30. 'Environment :: Web Environment',
  31. 'Intended Audience :: Developers',
  32. 'License :: OSI Approved :: BSD License',
  33. 'Operating System :: OS Independent',
  34. 'Programming Language :: Python',
  35. 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
  36. 'Topic :: Software Development :: Libraries :: Python Modules'
  37. ]
  38. )

That’s a lot of code but you can really just copy/paste that from existingextensions and adapt.

flask_sqlite3.py

Now this is where your extension code goes. But how exactly should suchan extension look like? What are the best practices? Continue readingfor some insight.

Initializing Extensions

Many extensions will need some kind of initialization step. For example,consider an application that’s currently connecting to SQLite like thedocumentation suggests (Using SQLite 3 with Flask). So how does the extensionknow the name of the application object?

Quite simple: you pass it to it.

There are two recommended ways for an extension to initialize:

initialization functions:

If your extension is called helloworld you might have a functioncalled init_helloworld(app[, extra_args]) that initializes theextension for that application. It could attach before / afterhandlers etc.

classes:

Classes work mostly like initialization functions but can later beused to further change the behavior. For an example look at how theOAuth extension works: there is an OAuth object that providessome helper functions like OAuth.remote_app to create a reference toa remote application that uses OAuth.

What to use depends on what you have in mind. For the SQLite 3 extensionwe will use the class-based approach because it will provide users with anobject that handles opening and closing database connections.

When designing your classes, it’s important to make them easily reusableat the module level. This means the object itself must not under anycircumstances store any application specific state and must be shareablebetween different applications.

The Extension Code

Here’s the contents of the flask_sqlite3.py for copy/paste:

  1. import sqlite3
  2. from flask import current_app, _app_ctx_stack
  3.  
  4.  
  5. class SQLite3(object):
  6. def __init__(self, app=None):
  7. self.app = app
  8. if app is not None:
  9. self.init_app(app)
  10.  
  11. def init_app(self, app):
  12. app.config.setdefault('SQLITE3_DATABASE', ':memory:')
  13. app.teardown_appcontext(self.teardown)
  14.  
  15. def connect(self):
  16. return sqlite3.connect(current_app.config['SQLITE3_DATABASE'])
  17.  
  18. def teardown(self, exception):
  19. ctx = _app_ctx_stack.top
  20. if hasattr(ctx, 'sqlite3_db'):
  21. ctx.sqlite3_db.close()
  22.  
  23. @property
  24. def connection(self):
  25. ctx = _app_ctx_stack.top
  26. if ctx is not None:
  27. if not hasattr(ctx, 'sqlite3_db'):
  28. ctx.sqlite3_db = self.connect()
  29. return ctx.sqlite3_db

So here’s what these lines of code do:

  • The init method takes an optional app object and, if supplied, willcall init_app.

  • The init_app method exists so that the SQLite3 object can beinstantiated without requiring an app object. This method supports thefactory pattern for creating applications. The init_app will set theconfiguration for the database, defaulting to an in memory database ifno configuration is supplied. In addition, the init_app methodattaches the teardown handler.

  • Next, we define a connect method that opens a database connection.

  • Finally, we add a connection property that on first access opensthe database connection and stores it on the context. This is alsothe recommended way to handling resources: fetch resources lazily thefirst time they are used.

Note here that we’re attaching our database connection to the topapplication context via _app_ctx_stack.top. Extensions should usethe top context for storing their own information with a sufficientlycomplex name.

So why did we decide on a class-based approach here? Because using ourextension looks something like this:

  1. from flask import Flask
  2. from flask_sqlite3 import SQLite3
  3.  
  4. app = Flask(__name__)
  5. app.config.from_pyfile('the-config.cfg')
  6. db = SQLite3(app)

You can then use the database from views like this:

  1. @app.route('/')def show_all(): cur = db.connection.cursor() cur.execute(…)

Likewise if you are outside of a request you can use the database bypushing an app context:

  1. with app.app_context():
  2. cur = db.connection.cursor()
  3. cur.execute(...)

At the end of the with block the teardown handles will be executedautomatically.

Additionally, the init_app method is used to support the factory patternfor creating apps:

  1. db = SQLite3()
  2. # Then later on.
  3. app = create_app('the-config.cfg')
  4. db.init_app(app)

Keep in mind that supporting this factory pattern for creating apps is requiredfor approved flask extensions (described below).

Note on init_app

As you noticed, init_app does not assign app to self. Thisis intentional! Class based Flask extensions must only store theapplication on the object when the application was passed to theconstructor. This tells the extension: I am not interested in usingmultiple applications.

When the extension needs to find the current application and it doesnot have a reference to it, it must either use thecurrent_app context local or change the API in a waythat you can pass the application explicitly.

Using _app_ctx_stack

In the example above, before every request, a sqlite3db variable isassigned to _app_ctx_stack.top. In a view function, this variable isaccessible using the connection property of SQLite3. During theteardown of a request, the sqlite3_db connection is closed. By usingthis pattern, the _same connection to the sqlite3 database is accessibleto anything that needs it for the duration of the request.

Learn from Others

This documentation only touches the bare minimum for extension development.If you want to learn more, it’s a very good idea to check out existing extensionson the PyPI. If you feel lost there is still the mailinglist and theIRC channel to get some ideas for nice looking APIs. Especially if you dosomething nobody before you did, it might be a very good idea to get some moreinput. This not only generates useful feedback on what people might want froman extension, but also avoids having multiple developers working in isolationon pretty much the same problem.

Remember: good API design is hard, so introduce your project on themailing list, and let other developers give you a helping hand withdesigning the API.

The best Flask extensions are extensions that share common idioms for theAPI. And this can only work if collaboration happens early.

Approved Extensions

Flask also has the concept of approved extensions. Approved extensionsare tested as part of Flask itself to ensure extensions do not break onnew releases. If you want your own extension to be approved you have tofollow these guidelines:

  • An approved Flask extension requires a maintainer. In the event anextension author would like to move beyond the project, the project shouldfind a new maintainer including full source hosting transition and PyPIaccess. If no maintainer is available, give access to the Flask core team.

  • An approved Flask extension must provide exactly one package or modulenamed flask_extensionname.

  • It must ship a testing suite that can either be invoked with make testor python setup.py test. For test suites invoked with maketest the extension has to ensure that all dependencies for the testare installed automatically. If tests are invoked with python setup.pytest, test dependencies can be specified in the setup.py file.The test suite also has to be part of the distribution.

  • APIs of approved extensions will be checked for the followingcharacteristics:

  • an approved extension has to support multiple applicationsrunning in the same Python process.

  • it must be possible to use the factory pattern for creatingapplications.

  • The license must be BSD/MIT/WTFPL licensed.

  • The naming scheme for official extensions is Flask-ExtensionName orExtensionName-Flask.

  • Approved extensions must define all their dependencies in thesetup.py file unless a dependency cannot be met because it is notavailable on PyPI.

  • The documentation must use the flask theme from theOfficial Pallets Themes.

  • The setup.py description (and thus the PyPI description) has tolink to the documentation, website (if there is one) and theremust be a link to automatically install the development version(PackageName==dev).

  • The zip_safe flag in the setup script must be set to False,even if the extension would be safe for zipping.

  • An extension currently has to support Python 3.4 and newer and 2.7.