Skip to content
Myr

Modernized docs in a few places

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 e2d4aa1 · Browse files · 3 files+29 added−18 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+29 added−18 removed

  • other:+29 added−18 removed

3 files+29 added−18 removed

modified:docs/patterns/sqlite3.rst+12 added-8 removed
8 unmodified lines
910Here is a simple example of how you can use SQLite 3 with Flask::1112 import sqlite313 from flask import _app_ctx_stack13 from flask import g1415 DATABASE = '/path/to/database.db'1617 def get_db():18 top = _app_ctx_stack.top19 if not hasattr(top, 'sqlite_db'):20 top.sqlite_db = sqlite3.connect(DATABASE)21 return top.sqlite_db18 db = getattr(g, '_database', None)19 if db is None:20 db = g._database = connect_to_database()21 return db2223 @app.teardown_appcontext24 def close_connection(exception):25 top = _app_ctx_stack.top26 if hasattr(top, 'sqlite_db'):27 top.sqlite_db.close()25 db = getattr(g, '_database', None)26 if db is not None:27 db.close()2829All the application needs to do in order to now use the database is having30an active application context (which is always true if there is an request31in flight) or to create an application context itself. At that point the32``get_db`` function can be used to get the current database connection.33Whenever the context is destroyed the database connection will be34terminated.3536Note: if you use Flask 0.9 or older you need to use37``flask._app_ctx_stack.top`` instead of ``g`` as the :data:`flask.g`38object was bound to the request and not application context.3940Example::4142 @app.route('/')43 def index():
95 unmodified lines
modified:docs/tutorial/dbcon.rst+6 added-1 removed
16 unmodified lines
17 g.db = connect_db()1819 @app.teardown_request20 def teardown_request(exception):21 db = getattr(g, 'db', None)22 if db is not None:21 g.db.close()23 db.close()2425Functions marked with :meth:`~flask.Flask.before_request` are called before26a request and passed no arguments. Functions marked with27:meth:`~flask.Flask.after_request` are called after a request and
11 unmodified lines
39things on other objects because this would not work with threaded40environments. That special :data:`~flask.g` object does some magic behind41the scenes to ensure it does the right thing.4243For an even better way to handle such resources see the :ref:`slite3`44documentation.4546Continue to :ref:`tutorial-views`.4748.. hint:: Where do I put this code?49
13 unmodified lines
modified:docs/appcontext.rst+11 added-9 removed
80 unmodified lines
81moves between threads and it will not be shared between requests. As such82it is the perfect place to store database connection information and other83things. The internal stack object is called :data:`flask._app_ctx_stack`.84Extensions are free to store additional information on the topmost level,85assuming they pick a sufficiently unique name.85assuming they pick a sufficiently unique name and should put there86information there, instead on the :data:`flask.g` object which is reserved87for user code.8889For more information about that, see :ref:`extension-dev`.9091Context Usage
16 unmodified lines
108109This is an example that connects to a database::110111 import sqlite3110 from flask import _app_ctx_stack112 from flask import g113114 def get_db():113 top = _app_ctx_stack.top114 if not hasattr(top, 'database'):115 top.database = connect_to_database()116 return top.database115 db = getattr(g, '_database', None)116 if db is None:117 db = g._database = connect_to_database()118 return db119120 @app.teardown_appcontext121 def teardown_db(exception):120 top = _app_ctx_stack.top121 if hasattr(top, 'database'):122 top.database.close()122 db = getattr(g, '_database', None)123 if db is not None:124 db.close()125126The first time ``get_db()`` is called the connection will be established.127To make this implicit a :class:`~werkzeug.local.LocalProxy` can be used::128
5 unmodified lines