Skip to content
Myr

Merge pull request #1862 from antsar/master

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

Add subclassing pattern/example

authored by Jeff Widman · committed by Jeff Widman · parents db299c0, 63b5dab · Browse files · 3 files+24 added−1 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+24 added−1 removed

  • other:+24 added−1 removed

3 files+24 added−1 removed

modified:docs/patterns/index.rst+1 added
39 unmodified lines
40 deferredcallbacks41 methodoverrides42 requestchecksum43 celery44 subclassing
added:docs/patterns/subclassing.rst+22 added
1Subclassing Flask2=================34The :class:`~flask.Flask` class is designed for subclassing.56One reason to subclass would be customizing the Jinja2 :class:`~jinja2.Environment`. For example, to add a new global template variable::78 from flask import Flask9 from datetime import datetime1011 class MyFlask(Flask):12 """ Flask with more global template vars """1314 def create_jinja_environment(self):15 """ Initialize my custom Jinja environment. """16 jinja_env = super(MyFlask, self).create_jinja_environment(self)17 jinja_env.globals.update(18 current_time = datetime.datetime.now()19 )20 return jinja_env2122This is the recommended approach for overriding or augmenting Flask's internal functionality.
modified:docs/becomingbig.rst+1 added-1 removed
33 unmodified lines
3435The :class:`~flask.Flask` class has many methods designed for subclassing. You36can quickly add or customize behavior by subclassing :class:`~flask.Flask` (see37the linked method docs) and using that subclass wherever you instantiate an38application class. This works well with :ref:`app-factories`.38application class. This works well with :ref:`app-factories`. See :doc:`/patterns/subclassing` for an example.3940Wrap with middleware.41---------------------42
59 unmodified lines